Search code examples
javaurlslashtrailing

Remove a trailing slash from a string(changed from url type) in JAVA


I want to remove the trailing slash from a string in Java.

I want to check if the string ends with a url, and if it does, i want to remove it.

Here is what I have:

String s = "http://almaden.ibm.com/";

s= s.replaceAll("/","");

and this:

String s = "http://almaden.ibm.com/";
length  =  s.length();
--length;
Char buff = s.charAt((length);
if(buff == '/')
{
     LOGGER.info("ends with trailing slash");
/*how to remove?*/
}
else  LOGGER.info("Doesnt end with trailing slash");

But neither work.


Solution

  • You can achieve this with Apache Commons StringUtils as follows:

    String s = "http://almaden.ibm.com/";
    StringUtils.removeEnd(s, "/")