How can I replace all line breaks from a string in Java in such a way that will work on Windows and Linux (ie no OS specific problems of carriage return/line feed/new line etc.)?
如何替换Java中的字符串中的所有换行符,使其在Windows和Linux上都能工作(即没有操作系统特定的回车/换行/换行等问题)?
I've tried (note readFileAsString is a function that reads a text file into a String):
我试过(注意readFileAsString是一个将文本文件读入字符串的函数):
String text = readFileAsString("textfile.txt");
text.replace("\n", "");
but this doesn't seem to work.
但这似乎行不通。
How can this be done?
怎么做呢?
16 个解决方案
#1
333
You need to set text
to the results of text.replace()
:
需要将文本设置为text.replace():
String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "");
This is necessary because Strings are immutable -- calling replace
doesn't change the original String, it returns a new one that's been changed. If you don't assign the result to text
, then that new String is lost and garbage collected.
这是必要的,因为字符串是不可变的——调用replace不会更改原始字符串,它会返回一个已更改的新字符串。如果不将结果赋给文本,那么新的字符串将丢失,并将垃圾收集。
As for getting the newline String for any environment -- that is available by calling System.getProperty("line.separator")
.
至于获取任何环境的换行字符串——可以通过调用System.getProperty(“line.separator”)获得。
#2
185
As noted in other answers, your code is not working primarily because String.replace(...)
does not change the target String. (It can't - Java strings are immutable!) What it actually does is creates a new String with the characters changed as required. But your code then throws away that String ...
如其他答案所示,您的代码不能正常工作,主要是因为String.replace(…)不会更改目标字符串。(它不能——Java字符串是不可变的!)它实际做的是创建一个新的字符串,字符根据需要更改。但是你的代码会丢弃那个字符串……
Here are some possible solutions. Which one is most correct depends on what exactly you are trying to do.
这里有一些可能的解决方案。哪个是最正确的取决于你到底想做什么。
// #1
text = text.replace("\n", "");
Simply removes all the newline characters. This does not cope with Windows or Mac line terminations.
只需删除所有换行字符。这并不适用于Windows或Mac线终端。
// #2
text = text.replace(System.getProperty("line.separator"), "");
Removes all line terminators for the current platform. This does not cope with the case where you are trying to process (for example) a UNIX file on Windows, or vice versa.
删除当前平台的所有线路终止符。这不能处理您试图在Windows上处理(例如)UNIX文件的情况,反之亦然。
// #3
text = text.replaceAll("\\r|\\n", "");
Removes all Windows, UNIX or Mac line terminators. However, if the input file is text, this will concatenate words; e.g.
删除所有Windows、UNIX或Mac线终端。但是,如果输入文件是文本,这将连接单词;如。
Goodbye cruel
world.
becomes
就变成了
Goodbye cruelworld.
So you might actually want to do this:
你可能想这样做
// #4
text = text.replaceAll("\\r\\n|\\r|\\n", " ");
which replaces each line terminator with a space.
用空格替换每个行结束符。
#3
18
If you want to remove only line terminators that are valid on the current OS, you could do this:
如果您想只删除当前操作系统上有效的行终止符,您可以这样做:
text = text.replaceAll(System.getProperty("line.separator"), "");
If you want to make sure you remove any line separators, you can do it like this:
如果你想要删除任何线分隔符,你可以这样做:
text = text.replaceAll("\\r|\\n", "");
Or, slightly more verbose, but less regexy:
或者,略显冗长,但更少的regexy:
text = text.replaceAll("\\r", "").replaceAll("\\n", "");
#4
9
str = str.replaceAll("\\r\\n|\\r|\\n", " ");
Worked perfectly for me after searching a lot, having failed with every other line.
在搜索了很多之后,我的工作做得很完美,其他的每一行都失败了。
#5
7
This would be efficient I guess
我想这会很有效率
String s;
s = "try this\n try me.";
s.replaceAll("[\\r\\n]+", "")
edited for syntax highlight
编辑语法突出显示
#6
6
Linebreaks are not the same under windows/linux/mac. You should use System.getProperties with the attribute line.separator.
在windows/linux/mac下换行符是不一样的。您应该使用系统。使用属性line.separator的getProperties。
#7
5
This function normalizes down all whitespace, including line breaks, to single spaces. Not exactly what the original question asked for, but likely to do exactly what is needed in many cases:
该函数将所有空格(包括换行符)规范化为单个空格。不完全是原始问题的要求,但很可能在很多情况下都是需要的:
import org.apache.commons.lang3.StringUtils;
final String cleansedString = StringUtils.normalizeSpace(rawString);
#8
2
String text = readFileAsString("textfile.txt").replace("\n","");
.replace returns a new string, strings in Java are Immutable.
.replace返回一个新字符串,Java中的字符串是不可变的。
#9
2
You may want to read your file with a BufferedReader
. This class can break input into individual lines, which you can assemble at will. The way BufferedReader
operates recognizes line ending conventions of the Linux, Windows and MacOS worlds automatically, regardless of the current platform.
您可能希望使用BufferedReader来读取文件。这个类可以将输入拆分为单独的行,您可以随意组合它们。BufferedReader的操作方式可以自动识别Linux、Windows和MacOS世界的行尾约定,而不管当前的平台是什么。
Hence:
因此:
BufferedReader br = new BufferedReader(
new InputStreamReader("textfile.txt"));
StringBuilder sb = new StringBuilder();
for (;;) {
String line = br.readLine();
if (line == null)
break;
sb.append(line);
sb.append(' '); // SEE BELOW
}
String text = sb.toString();
Note that readLine()
does not include the line terminator in the returned string. The code above appends a space to avoid gluing together the last word of a line and the first word of the next line.
注意,readLine()在返回的字符串中不包含行终止符。上面的代码附加了一个空格,以避免将一行的最后一个单词和下一行的第一个单词粘在一起。
#10
2
String text = readFileAsString("textfile.txt").replaceAll("\n", "");
Even though the definition of trim() in oracle website is "Returns a copy of the string, with leading and trailing whitespace omitted."
尽管oracle网站中的trim()的定义是“返回字符串的一个副本,省略了前导和后导空格”。
the documentation omits to say that new line characters (leading and trailing) will also be removed.
文档中省略了新的行字符(引导和结尾)也将被删除。
In short String text = readFileAsString("textfile.txt").trim();
will also work for you. (Checked with Java 6)
在短字符串中,text = readFileAsString("textfile.txt").trim();也会为你工作。与Java 6(检查)
#11
0
I find it odd that (Apache) StringUtils wasn't covered here yet.
我觉得奇怪的是(Apache) StringUtils还没有被覆盖。
you can remove all newlines (or any other occurences of a substring for that matter) from a string using the .replace
method
您可以使用.replace方法从字符串中删除所有换行(或从子字符串中发生的任何其他事件)。
StringUtils.replace(myString, "\n", "");
This line will replace all newlines with the empty string.
这一行将用空字符串替换所有的新行。
because newline is technically a character you can optionally use the .replaceChars
method that will replace characters
因为从技术上讲,换行是一个字符,所以可以选择使用.replaceChars方法替换字符
StringUtils.replaceChars(myString, '\n', '');
#12
0
FYI if you can want to replace simultaneous muti-linebreaks with single line break then you can use
简单地说,如果您想要用单线中断替换同时的多线中断,那么您可以使用
myString.trim().replaceAll("[\n]{2,}", "\n")
Or replace with a single space
或者用一个空格替换
myString.trim().replaceAll("[\n]{2,}", " ")
#13
0
You can use apache commons IOUtils to iterate through the line and append each line to StringBuilder. And don't forget to close the InputStream
您可以使用apache commons IOUtils迭代这一行,并将每一行附加到StringBuilder。不要忘记关闭InputStream。
StringBuilder sb = new StringBuilder();
FileInputStream fin=new FileInputStream("textfile.txt");
LineIterator lt=IOUtils.lineIterator(fin, "utf-8");
while(lt.hasNext())
{
sb.append(lt.nextLine());
}
String text = sb.toString();
IOUtils.closeQuitely(fin);
#14
0
You can use generic methods to replace any char with any char.
您可以使用通用方法将任何字符替换为任何字符。
public static void removeWithAnyChar(String str, char replceChar,
char replaceWith) {
char chrs[] = str.toCharArray();
int i = 0;
while (i < chrs.length) {
if (chrs[i] == replceChar) {
chrs[i] = replaceWith;
}
i++;
}
}
#15
0
org.apache.commons.lang.StringUtils#chopNewline
org.apache.commons.lang.StringUtils # chopNewline
#16
-2
Try doing this:
试着这样做:
textValue= textValue.replaceAll("\n", "");
textValue= textValue.replaceAll("\t", "");
textValue= textValue.replaceAll("\\n", "");
textValue= textValue.replaceAll("\\t", "");
textValue= textValue.replaceAll("\r", "");
textValue= textValue.replaceAll("\\r", "");
textValue= textValue.replaceAll("\r\n", "");
textValue= textValue.replaceAll("\\r\\n", "");
#1
333
You need to set text
to the results of text.replace()
:
需要将文本设置为text.replace():
String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "");
This is necessary because Strings are immutable -- calling replace
doesn't change the original String, it returns a new one that's been changed. If you don't assign the result to text
, then that new String is lost and garbage collected.
这是必要的,因为字符串是不可变的——调用replace不会更改原始字符串,它会返回一个已更改的新字符串。如果不将结果赋给文本,那么新的字符串将丢失,并将垃圾收集。
As for getting the newline String for any environment -- that is available by calling System.getProperty("line.separator")
.
至于获取任何环境的换行字符串——可以通过调用System.getProperty(“line.separator”)获得。
#2
185
As noted in other answers, your code is not working primarily because String.replace(...)
does not change the target String. (It can't - Java strings are immutable!) What it actually does is creates a new String with the characters changed as required. But your code then throws away that String ...
如其他答案所示,您的代码不能正常工作,主要是因为String.replace(…)不会更改目标字符串。(它不能——Java字符串是不可变的!)它实际做的是创建一个新的字符串,字符根据需要更改。但是你的代码会丢弃那个字符串……
Here are some possible solutions. Which one is most correct depends on what exactly you are trying to do.
这里有一些可能的解决方案。哪个是最正确的取决于你到底想做什么。
// #1
text = text.replace("\n", "");
Simply removes all the newline characters. This does not cope with Windows or Mac line terminations.
只需删除所有换行字符。这并不适用于Windows或Mac线终端。
// #2
text = text.replace(System.getProperty("line.separator"), "");
Removes all line terminators for the current platform. This does not cope with the case where you are trying to process (for example) a UNIX file on Windows, or vice versa.
删除当前平台的所有线路终止符。这不能处理您试图在Windows上处理(例如)UNIX文件的情况,反之亦然。
// #3
text = text.replaceAll("\\r|\\n", "");
Removes all Windows, UNIX or Mac line terminators. However, if the input file is text, this will concatenate words; e.g.
删除所有Windows、UNIX或Mac线终端。但是,如果输入文件是文本,这将连接单词;如。
Goodbye cruel
world.
becomes
就变成了
Goodbye cruelworld.
So you might actually want to do this:
你可能想这样做
// #4
text = text.replaceAll("\\r\\n|\\r|\\n", " ");
which replaces each line terminator with a space.
用空格替换每个行结束符。
#3
18
If you want to remove only line terminators that are valid on the current OS, you could do this:
如果您想只删除当前操作系统上有效的行终止符,您可以这样做:
text = text.replaceAll(System.getProperty("line.separator"), "");
If you want to make sure you remove any line separators, you can do it like this:
如果你想要删除任何线分隔符,你可以这样做:
text = text.replaceAll("\\r|\\n", "");
Or, slightly more verbose, but less regexy:
或者,略显冗长,但更少的regexy:
text = text.replaceAll("\\r", "").replaceAll("\\n", "");
#4
9
str = str.replaceAll("\\r\\n|\\r|\\n", " ");
Worked perfectly for me after searching a lot, having failed with every other line.
在搜索了很多之后,我的工作做得很完美,其他的每一行都失败了。
#5
7
This would be efficient I guess
我想这会很有效率
String s;
s = "try this\n try me.";
s.replaceAll("[\\r\\n]+", "")
edited for syntax highlight
编辑语法突出显示
#6
6
Linebreaks are not the same under windows/linux/mac. You should use System.getProperties with the attribute line.separator.
在windows/linux/mac下换行符是不一样的。您应该使用系统。使用属性line.separator的getProperties。
#7
5
This function normalizes down all whitespace, including line breaks, to single spaces. Not exactly what the original question asked for, but likely to do exactly what is needed in many cases:
该函数将所有空格(包括换行符)规范化为单个空格。不完全是原始问题的要求,但很可能在很多情况下都是需要的:
import org.apache.commons.lang3.StringUtils;
final String cleansedString = StringUtils.normalizeSpace(rawString);
#8
2
String text = readFileAsString("textfile.txt").replace("\n","");
.replace returns a new string, strings in Java are Immutable.
.replace返回一个新字符串,Java中的字符串是不可变的。
#9
2
You may want to read your file with a BufferedReader
. This class can break input into individual lines, which you can assemble at will. The way BufferedReader
operates recognizes line ending conventions of the Linux, Windows and MacOS worlds automatically, regardless of the current platform.
您可能希望使用BufferedReader来读取文件。这个类可以将输入拆分为单独的行,您可以随意组合它们。BufferedReader的操作方式可以自动识别Linux、Windows和MacOS世界的行尾约定,而不管当前的平台是什么。
Hence:
因此:
BufferedReader br = new BufferedReader(
new InputStreamReader("textfile.txt"));
StringBuilder sb = new StringBuilder();
for (;;) {
String line = br.readLine();
if (line == null)
break;
sb.append(line);
sb.append(' '); // SEE BELOW
}
String text = sb.toString();
Note that readLine()
does not include the line terminator in the returned string. The code above appends a space to avoid gluing together the last word of a line and the first word of the next line.
注意,readLine()在返回的字符串中不包含行终止符。上面的代码附加了一个空格,以避免将一行的最后一个单词和下一行的第一个单词粘在一起。
#10
2
String text = readFileAsString("textfile.txt").replaceAll("\n", "");
Even though the definition of trim() in oracle website is "Returns a copy of the string, with leading and trailing whitespace omitted."
尽管oracle网站中的trim()的定义是“返回字符串的一个副本,省略了前导和后导空格”。
the documentation omits to say that new line characters (leading and trailing) will also be removed.
文档中省略了新的行字符(引导和结尾)也将被删除。
In short String text = readFileAsString("textfile.txt").trim();
will also work for you. (Checked with Java 6)
在短字符串中,text = readFileAsString("textfile.txt").trim();也会为你工作。与Java 6(检查)
#11
0
I find it odd that (Apache) StringUtils wasn't covered here yet.
我觉得奇怪的是(Apache) StringUtils还没有被覆盖。
you can remove all newlines (or any other occurences of a substring for that matter) from a string using the .replace
method
您可以使用.replace方法从字符串中删除所有换行(或从子字符串中发生的任何其他事件)。
StringUtils.replace(myString, "\n", "");
This line will replace all newlines with the empty string.
这一行将用空字符串替换所有的新行。
because newline is technically a character you can optionally use the .replaceChars
method that will replace characters
因为从技术上讲,换行是一个字符,所以可以选择使用.replaceChars方法替换字符
StringUtils.replaceChars(myString, '\n', '');
#12
0
FYI if you can want to replace simultaneous muti-linebreaks with single line break then you can use
简单地说,如果您想要用单线中断替换同时的多线中断,那么您可以使用
myString.trim().replaceAll("[\n]{2,}", "\n")
Or replace with a single space
或者用一个空格替换
myString.trim().replaceAll("[\n]{2,}", " ")
#13
0
You can use apache commons IOUtils to iterate through the line and append each line to StringBuilder. And don't forget to close the InputStream
您可以使用apache commons IOUtils迭代这一行,并将每一行附加到StringBuilder。不要忘记关闭InputStream。
StringBuilder sb = new StringBuilder();
FileInputStream fin=new FileInputStream("textfile.txt");
LineIterator lt=IOUtils.lineIterator(fin, "utf-8");
while(lt.hasNext())
{
sb.append(lt.nextLine());
}
String text = sb.toString();
IOUtils.closeQuitely(fin);
#14
0
You can use generic methods to replace any char with any char.
您可以使用通用方法将任何字符替换为任何字符。
public static void removeWithAnyChar(String str, char replceChar,
char replaceWith) {
char chrs[] = str.toCharArray();
int i = 0;
while (i < chrs.length) {
if (chrs[i] == replceChar) {
chrs[i] = replaceWith;
}
i++;
}
}
#15
0
org.apache.commons.lang.StringUtils#chopNewline
org.apache.commons.lang.StringUtils # chopNewline
#16
-2
Try doing this:
试着这样做:
textValue= textValue.replaceAll("\n", "");
textValue= textValue.replaceAll("\t", "");
textValue= textValue.replaceAll("\\n", "");
textValue= textValue.replaceAll("\\t", "");
textValue= textValue.replaceAll("\r", "");
textValue= textValue.replaceAll("\\r", "");
textValue= textValue.replaceAll("\r\n", "");
textValue= textValue.replaceAll("\\r\\n", "");