Richard Chan Richard Chan - 2 months ago 14
Java Question

Simple way to remove all slashes at the end of url in java

I need to remove all slashed at the end of url,and i don't known how many slashes at the end.

for example

http://www.example.com/user//


after removing slashe at the end

http://www.example.com/user


are there any simple ways like regular expression to do that.

Answer

use String.replaceAll ("\\/$", "");

The $ means that it is at the end of the String

As per @WiktorStribiżew to remove multiple slashes

use String.replaceAll ("/+$", "");

Comments