从 Java 字符串的末尾删除行尾字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3100585/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:21:48  来源:igfitidea点击:

Remove end of line characters from end of Java String

javaregexstring

提问by Iain Sproat

I have a string which I'd like to remove the end of line characters from the very end of the string only using Java

我有一个字符串,我想仅使用 Java 从字符串的末尾删除行尾字符

"foo\r\nbar\r\nhello\r\nworld\r\n"

which I'd like to become

我想成为

"foo\r\nbar\r\nhello\r\nworld"

(This question is similar to, but not the same as question 593671)

(此问题与问题593671相似,但不相同)

回答by polygenelubricants

You can use s = s.replaceAll("[\r\n]+$", "");. This trims the \rand \ncharacters at the end of the string

您可以使用s = s.replaceAll("[\r\n]+$", "");. 这修剪\r\n字符在字符串的结尾

The regex is explained as follows:

正则表达式解释如下:

  • [\r\n]is a character class containing \rand \n
  • +is one-or-more repetition of
  • $is the end-of-string anchor
  • [\r\n]是一个包含\r和的字符类\n
  • +是一次或多次重复
  • $是字符串结尾的锚点

References

参考



Related topics

相关话题

You can also use String.trim()to trim anywhitespace characters from the beginningand endof the string:

您还可以使用从字符串的开头结尾String.trim()修剪任何空白字符:

s = s.trim();

If you need to check if a Stringcontains nothing but whitespace characters, you can check if it isEmpty()after trim():

如果您需要检查 a 是否只String包含空白字符,您可以isEmpty()在以下之后检查它trim()

if (s.trim().isEmpty()) {
   //...
}

Alternatively you can also see if it matches("\\s*"), i.e. zero-or-more of whitespace characters. Note that in Java, the regex matchestries to match the wholestring. In flavors that can match a substring, you need to anchor the pattern, so it's ^\s*$.

或者,您也可以查看它是否存在matches("\\s*"),即零个或多个空白字符。请注意,在 Java 中,正则表达式会matches尝试匹配整个字符串。在可以匹配子字符串的风格中,你需要锚定模式,所以它是^\s*$.

Related questions

相关问题

回答by Richard Walton

Wouldn't String.trimdo the trick here?

不会String.trim做的伎俩吗?

i.e you'd call the method .trim()on your string and it should return a copy of that string minus any leading or trailing whitespace.

即你会.trim()在你的字符串上调用该方法,它应该返回该字符串的副本,减去任何前导或尾随空格。

回答by Stephen C

The Apache Commons Lang StringUtils.stripEnd(String str, String stripChars)will do the trick; e.g.

Apache Commons LangStringUtils.stripEnd(String str, String stripChars)可以解决这个问题;例如

    String trimmed = StringUtils.stripEnd(someString, "\n\r");

If you want to remove allwhitespace at the end of the String:

如果要删除字符串末尾的所有空格:

    String trimmed = StringUtils.stripEnd(someString, null);

回答by bezmax

Well, everyone gave some way to do it with regex, so I'll give a fastest way possible instead:

好吧,每个人都给出了一些用正则表达式来做的方法,所以我会给出一个最快的方法:

public String replace(String val) {
    for (int i=val.length()-1;i>=0;i--) {
        char c = val.charAt(i);
        if (c != '\n' && c != '\r') {
            return val.substring(0, i+1);
        }
    }
    return "";
}

Benchmark says it operates ~45 times faster than regexp solutions.

Benchmark 表示它的运行速度比 regexp 解决方案快约 45 倍。

回答by Cowan

If you have Google's guava-librariesin your project (if not, you arguably should!) you'd do this with a CharMatcher:

如果你的项目中有谷歌的番石榴库(如果没有,你可以说应该!)你会用一个CharMatcher 来做到这一点

String result = CharMatcher.any("\r\n").trimTrailingFrom(input);

回答by Jesper

String text = "foo\r\nbar\r\nhello\r\nworld\r\n";
String result = text.replaceAll("[\r\n]+$", "");

回答by Favonius

"foo\r\nbar\r\nhello\r\nworld\r\n".replaceAll("\s+$", "")
or
"foo\r\nbar\r\nhello\r\nworld\r\n".replaceAll("[\r\n]+$", "")