TrimEnd for Java?
There isn't a direct replacement. You can use regexps, or perhaps Commons Lang StringUtils.stripEnd() method.
Since Java 11, you can use "string".stripTrailing() to strip off trailing whitespace, or "string".stripLeading(). If you need the more generic version that strips off the specified characters, then see below, there is no direct replacement in Java 11.
Leaving the old answer here for pre-Java 11 versions:
There is no direct equivalent, however if you want to strip trailing whitespace you can use:
"string".replaceAll("\\s+$", "");
\s
is the regular expression character set "whitespace", if you only care about stripping trailing spaces, you can replace it with the space character. If you want to use the more general form of trimEnd(), that is to remove an arbitrary set of trailing characters then you need to modify it slightly to use:
"string".replaceAll("[" + characters + "]+$", "");
Do be aware that the first argument is a generic regex, and so if one of your characters
is the ]
then this needs to be the first character in your list. For a full description of the java regex patterns, look at the javadoc.
Should you ever need to implement the trimstart() method from .net, it is almost the same, but would look like this:
"string".replaceAll("^[" + characters + "]+", "");