用于 Java 的 TrimEnd?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2143782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TrimEnd for Java?
提问by jasonh
In the process of converting a C# application to Java, I came across the use of String's TrimEndmethod. Is there an equivalent Java implementation? I can't seem to find one.
在将 C# 应用程序转换为 Java 的过程中,我遇到了使用 String 的TrimEnd方法。是否有等效的 Java 实现?我似乎找不到一个。
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.
我宁愿不将其替换为trim,因为此时我不想更改程序的含义或操作,除非必须这样做。
回答by Paul Wagland
There is no direct equivalent, however if you want to strip trailing whitespace you can use:
没有直接的等价物,但是如果你想去掉尾随的空格,你可以使用:
"string".replaceAll("\s+$", "");
\sis 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:
\s是正则表达式字符集“空白”,如果只关心去掉尾随空格,可以用空格字符代替。如果您想使用更通用的trimEnd()形式,即删除任意一组尾随字符,那么您需要稍微修改它以使用:
"string".replaceAll("[" + characters + "]+$", "");
Do be aware that the first argument is a generic regex, and so if one of your charactersis 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.
请注意,第一个参数是通用正则表达式,因此如果您的其中一个characters是,]那么这需要是列表中的第一个字符。有关 java regex 模式的完整描述,请查看javadoc。
Should you ever need to implement the trimstart()method from .net, it is almost the same, but would look like this:
如果你需要从 .net实现trimstart()方法,它几乎是一样的,但看起来像这样:
"string".replaceAll("^[" + characters + "]+", "");
回答by Brian Agnew
There isn't a direct replacement. You can use regexps, or perhaps Commons Lang StringUtils.stripEnd()method.
没有直接替代品。您可以使用正则表达式,或者 Commons LangStringUtils.stripEnd()方法。
回答by user1285221
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);
}
回答by YujiSoftware
Good news!
It was added in Java 11.
[JDK-8200378] String::strip, String::stripLeading, String::stripTrailing - Java Bug System
好消息!
它是在 Java 11 中添加的。
[JDK-8200378] String::strip, String::stripLeading, String::stripTrailing - Java 错误系统
strip()stripLeading()stripTrailing()
strip()stripLeading()stripTrailing()
回答by Jon Onstott
Here is a trick to do it from: http://www.overclock.net/coding-programming/320937-simple-java-trim-help.html
这是一个技巧:http: //www.overclock.net/coding-programming/320937-simple-java-trim-help.html
str = str.replaceAll(" +$", "");
回答by kroiz
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;
}
回答by Nikolay Mikhaylov
If performance is not important one-liner could be used:
如果性能不重要,可以使用 one-liner:
("X" + str).trim().substring(1)
回答by Xanas
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.
如果您使用的是 maven,您可以将它简单地添加到 pom.xml 的依赖项部分。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>

