Skip to content

Instantly share code, notes, and snippets.

@musubu
Created July 11, 2012 01:18
Show Gist options
  • Save musubu/3087294 to your computer and use it in GitHub Desktop.
Save musubu/3087294 to your computer and use it in GitHub Desktop.
remove a trailing slash from a string in Java
s = s.replaceAll("/$", "");
// or
s = s.replaceAll("/\\z", "");
// slightly faster
if (s.endsWith("/")) {
s = s.substring(0, s.length() - 1);
}
// slightly faster, but a bit ugly
s = s.substring(0, s.length() - (s.endsWith("/") ? 1 : 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment