jasonh
jasonh

Reputation: 30293

TrimEnd for Java?

In the process of converting a C# application to Java, I came across the use of String's TrimEnd method. Is there an equivalent Java implementation? I can't seem to find one.

I'd rather not replace it with trim, since I don't want to change the meaning or operation of the program at this point unless I have to.

Upvotes: 26

Views: 27917

Answers (8)

Paul Wagland
Paul Wagland

Reputation: 29116

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 + "]+", "");

Upvotes: 36

Brian Agnew
Brian Agnew

Reputation: 272247

There isn't a direct replacement. You can use regexps, or perhaps Commons Lang StringUtils.stripEnd() method.

Upvotes: 13

YujiSoftware
YujiSoftware

Reputation: 1602

Good news!
It was added in Java 11.
[JDK-8200378] String::strip, String::stripLeading, String::stripTrailing - Java Bug System

  • strip()
  • stripLeading()
  • stripTrailing()

Upvotes: 7

Xanas
Xanas

Reputation: 1

I like googles version from guava.

import static com.google.common.base.CharMatcher.WHITESPACE;

WHITESPACE.trimTrailingFrom(value);

or

CharMatcher.anyOf(suffix).trimTrailingFrom(value);

if you are using maven you can add it simply to your dependencies section of pom.xml.

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>23.0</version>
</dependency>

Upvotes: 0

Nikolay Mikhaylov
Nikolay Mikhaylov

Reputation: 68

If performance is not important one-liner could be used:

("X" + str).trim().substring(1)

Upvotes: 0

user1285221
user1285221

Reputation: 71

if you value performance (perhaps your method is used in a loop over thousands of strings), than you could use this method:

public String trimEnd(String value) {
    int len = value.length();
    int st = 0;
    while ((st < len) && value.charAt(len - 1) == ' ') {
      len--;
    }
    return value.substring(0, len);
}

Upvotes: 6

kroiz
kroiz

Reputation: 1772

There is no native equivalent. Use this:

public static String trimEnd( String s,  String suffix) {

 if (s.endsWith(suffix)) {

   return s.substring(0, s.length() - suffix.length());

 }
 return s;
}

Upvotes: 4

Jon Onstott
Jon Onstott

Reputation: 13727

Here is a trick to do it from: http://www.overclock.net/coding-programming/320937-simple-java-trim-help.html

str = str.replaceAll(" +$", "");

Upvotes: 2

Related Questions