Richard Chan Richard Chan - 1 year ago 59
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 Source

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 ("/+$", "");