java 如何在java中将“\\r\\n”更改为行分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11546146/
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
How to change "\\r\\n" to line separator in java
提问by r0dney
I am working on a school project to build a pseudo terminal and file system. The terminal is scanning System.in and pass the string to controller.
我正在做一个学校项目来构建一个伪终端和文件系统。终端正在扫描 System.in 并将字符串传递给控制器。
Input to console: abc\r\nabc\r\nabc
控制台输入: abc\r\nabc\r\nabc
Here is the code I tried
这是我试过的代码
Scanner systemIn = Scanner(System.in);
input = systemIn.nextLine();
input = input.replaceAll("\\r\\n",System.getProperty("line.separator"));
System.out.print(input);
I want java to treat the \r\n I typed to console as a line separator, not actually \ and r. What it does now is print the input as is.
我希望 java 将我输入到控制台的 \r\n 视为行分隔符,而不是实际的 \ 和 r。它现在所做的是按原样打印输入。
Desired Ouput:
所需输出:
abc
美国广播公司
abc
美国广播公司
abc
美国广播公司
UPDATE: I tried input = StringEscapeUtils.unescapeJava(input);
and it solved the problem.
更新:我试过了input = StringEscapeUtils.unescapeJava(input);
,它解决了这个问题。
回答by Bergi
You need to double-escape the regexes in java (once for the regex backslash, once for the Java string). You dont want a linebreak (/\n/
, "\\n"
), but a backslash (/\\/
) plus a "n": /\\n/
, "\\\\n"
. So this should work:
您需要对 Java 中的正则表达式进行双重转义(一次用于正则表达式反斜杠,一次用于 Java 字符串)。您不需要换行符 ( /\n/
, "\\n"
),而是反斜杠 ( /\\/
) 加“n”: /\\n/
, "\\\\n"
。所以这应该有效:
input.replaceAll("(\\r)?\\n", System.getProperty("line.separator"));
For a more broad handling of escape sequences see How to unescape a Java string literal in Java?
有关转义序列的更广泛处理,请参阅如何在 Java 中取消转义 Java 字符串文字?
回答by Soronthar
If your input has the string '\r\n', try this
如果你的输入有字符串 '\r\n',试试这个
Scanner systemIn = Scanner(System.in);
input = systemIn.nextLine();
input = input.replaceAll("\\r\\n",System.getProperty("line.separator"))
回答by Peter Lawrey
For consistent behaviour I would replace \\r with \r and \\n with \n rather than replace \\r\\n with the newline as this will have different behaviour on different systems.
对于一致的行为,我会将 \\r 替换为 \r 并将 \\n 替换为 \n 而不是将 \\r\\n 替换为换行符,因为这在不同的系统上会有不同的行为。
You can do
你可以做
input = systemIn.nextLine().replaceAll("\\r", "\r").replaceAll("\\n", "\n");
nextLine() strips of the newline at the end. If you want to add a line separator you can do
nextLine() 在末尾删除换行符。如果你想添加一个行分隔符,你可以这样做
input = systemIn.nextLine() + System.getProperty("line.separator");
if you are using println() you don't need to add it back.
如果您使用的是 println() ,则不需要将其添加回来。
System.out.println(systemIn.nextLine()); // prints a new line.
回答by Singagirl
As it was mentioned by r0dney, the Bergi's solution doesn't work.
正如 r0dney 所提到的,Bergi 的解决方案不起作用。
The ability to use some 3rd party libraries is good, however for a person who studies it is better to know the theory, because not for every problem exists some 3rd party library.
使用一些 3rd 方库的能力是好的,但是对于学习的人来说,了解理论更好,因为并非所有问题都存在一些 3rd 方库。
Overload project with tons of 3rd party libraries for tasks which can be solved in one line code makes project bulky and not easy maintainable. Anyway here is what's working:
用大量的 3rd 方库来重载可以在一行代码中解决的任务的项目,使得项目庞大且不易维护。无论如何,这是有效的:
content.replaceAll("(\\r)?\\n", System.getProperty("line.separator"));
回答by user207421
Unless you are actually typing \ and r and \ and n into the console, you don't need to do this at all: instead you have a major misunderstanding. The CR character is represented in a Stringas \r but it consists of only one byte with the hex value 0xD. And if you are typing backslashes into the console, the simple answer is "don't". Just hit the Enter key: that's what it's for. It will transmit the CR byte into your code.
除非您实际上是在控制台中键入 \ 和 r 以及 \ 和 n ,否则您根本不需要这样做:相反,您有一个重大误解。CR 字符在字符串中表示为 \r,但它仅由一个字节组成,十六进制值为 0xD。如果您在控制台中输入反斜杠,简单的答案是“不要”。只需按 Enter 键:这就是它的用途。它会将 CR 字节传输到您的代码中。