Assume there is a url="www.example.com/"
. Using the following code I want to remove the trailing slash but it leaves a blank at the end of string (which by the way I do not know why) and using the rest of the code, I am trying to remove the white space but it will not work.
String url="http://www.example.com/";
int slash=url.lastIndexOf("/");
StringBuilder myURL = new StringBuilder(url);
if(url.endsWith("/")){
myURL.setCharAt(slash, Character.MIN_VALUE );
url=myURL.toString();
}
url=url.replaceAll("\\s+","");
System.out.println(url);
Because
\s+
does not matchCharacter.MIN_VALUE
. Use' '
instead.But why don't you just remove the
/
?