There are two options: using pattern matching (slightly slower):
s = s.replaceAll("/$", "");
or:
s = s.replaceAll("/\\z", "");
And using an if statement (slightly faster):
if (s.endsWith("https://stackoverflow.com/")) {
s = s.substring(0, s.length() - 1);
}
or (a bit ugly):
s = s.substring(0, s.length() - (s.endsWith("https://stackoverflow.com/") ? 1 : 0));
Please note you need to use s = s...
, because Strings are immutable.