从 Java 字符串中删除行尾字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/593671/
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
Remove end of line characters from Java string
提问by
I have string like this
我有这样的字符串
"hello
java
book"
I want remove \r and \n from string(hello\r\njava\r\nbook). I want a string as "hellojavabook". How can I do this?
我想从 string(hello\r\njava\r\nbook) 中删除 \r 和 \n。我想要一个字符串作为“hellojavabook”。我怎样才能做到这一点?
回答by Beep beep
Given a String str:
给定一个字符串 str:
str = str.replaceAll("\\r","")
str = str.replaceAll("\\n","")
回答by Rob Lachlan
Have you tried using the replaceAllmethod to replace any occurence of \n or \r with the empty String?
您是否尝试过使用replaceAll方法将任何出现的 \n 或 \r 替换为空字符串?
回答by TofuBeer
Regex with replaceAll.
带有 replaceAll 的正则表达式。
public class Main
{
public static void main(final String[] argv)
{
String str;
str = "hello\r\njava\r\nbook";
str = str.replaceAll("(\r|\n)", "");
System.out.println(str);
}
}
If you only want to remove \r\n when they are pairs (the above code removes either \r or \n) do this instead:
如果您只想在成对时删除 \r\n(上面的代码删除了 \r 或 \n),请执行以下操作:
str = str.replaceAll("\r\n", "");
回答by Lawrence Dol
If you want to avoid the regex, or must target an earlier JVM, String.replace() will do:
如果您想避免使用正则表达式,或者必须针对较早的 JVM,则 String.replace() 将执行以下操作:
str=str.replace("\r","").replace("\n","");
And to remove a CRLF pair:
并删除一个 CRLF 对:
str=str.replace("\r\n","");
The latter is more efficient than building a regex to do the same thing. But I think the former will be faster as a regex since the string is only parsed once.
后者比构建正则表达式来做同样的事情更有效。但我认为前者作为正则表达式会更快,因为字符串只被解析一次。
回答by RazvanM
static byte[] discardWhitespace(byte[] data) {
byte groomedData[] = new byte[data.length];
int bytesCopied = 0;
for (int i = 0; i < data.length; i++) {
switch (data[i]) {
case (byte) '\n' :
case (byte) '\r' :
break;
default:
groomedData[bytesCopied++] = data[i];
}
}
byte packedData[] = new byte[bytesCopied];
System.arraycopy(groomedData, 0, packedData, 0, bytesCopied);
return packedData;
}
Code found on commons-codec project.
在 commons-codec 项目中找到的代码。
回答by Vikram Subramanian
Did you try
你试过了吗
string.trim();
This is meant to trim all leading and leaning while spaces in the string. Hope this helps.
这意味着在字符串中的空格时修剪所有前导和倾斜。希望这可以帮助。
Edit: (I was not explicit enough)
编辑:(我不够明确)
So, when you string.split(), you will have a string[] - for each of the strings in the array, do a string.trim() and then append it.
因此,当您使用 string.split() 时,您将有一个 string[] - 对于数组中的每个字符串,执行一个 string.trim() 然后附加它。
String[] tokens = yourString.split(" ");
StringBuffer buff = new StringBuffer();
for (String token : tokens)
{
buff.append(token.trim());
}
Use stringBuffer/Builder instead of appending in the same string.
使用 stringBuffer/Builder 而不是附加在同一个字符串中。
回答by ronicst
public static void main(final String[] argv)
{
String str;
str = "hello\r\n\tjava\r\nbook";
str = str.replaceAll("(\r|\n|\t)", "");
System.out.println(str);
}
It would be useful to add the tabulation in regex too.
在正则表达式中添加表格也会很有用。
回答by Naved Ali
You can either directly pass line terminator e.g. \n, if you know the line terminator in Windows, Mac or UNIX. Alternatively you can use following code to replace line breaks in either of three major operating system.
如果您知道 Windows、Mac 或 UNIX 中的行终止符,则可以直接传递行终止符,例如 \n。或者,您可以使用以下代码替换三个主要操作系统中的任何一个中的换行符。
str = str.replaceAll("\r\n|\r|\n", " ");
Above code line will replace line breaks with space in Mac, Windows and Linux. Also you can use line-separator. It will work for all OS. Below is the code snippet for line-separator.
上面的代码行将在 Mac、Windows 和 Linux 中用空格替换换行符。您也可以使用行分隔符。它适用于所有操作系统。下面是行分隔符的代码片段。
String lineSeparator=System.lineSeparator();
String newString=yourString.replace(lineSeparator, "");
回答by Srinivasan Sekar
You can use unescapeJava
from org.apache.commons.text.StringEscapeUtils
like below
您可以unescapeJava
从org.apache.commons.text.StringEscapeUtils
下面使用
str = "hello\r\njava\r\nbook";
StringEscapeUtils.unescapeJava(str);
回答by Abhishek Sengupta
Hey we can also use this regex solution.
嘿,我们也可以使用这个正则表达式解决方案。
String chomp = StringUtils.normalizeSpace(sentence.replaceAll("[\r\n]"," "));