简体   繁体   English

如何在Java中将“ \\\\ r \\\\ n”更改为行分隔符

[英]How to change “\\r\\n” to line separator in java

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. 我希望Java将我输入控制台的\\ r \\ n视为行分隔符,而不实际上是\\和r。 What it does now is print the input as is. 现在要做的是按原样打印输入。

Desired Ouput: 所需的Ouput:

abc abc

abc abc

abc abc

UPDATE: I tried input = StringEscapeUtils.unescapeJava(input); 更新:我尝试input = StringEscapeUtils.unescapeJava(input); and it solved the problem. 它解决了问题。

You need to double-escape the regexes in java (once for the regex backslash, once for the Java string). 您需要在Java中两次转义正则表达式(一次用于正则表达式反斜杠,一次用于Java字符串)。 You dont want a linebreak ( /\\n/ , "\\\\n" ), but a backslash ( /\\\\/ ) plus a "n": /\\\\n/ , "\\\\\\\\n" . 您不需要换行符( /\\n/"\\\\n" ),而是反斜杠( /\\\\/ )加上“ n”: /\\\\n/"\\\\\\\\n" So this should work: 所以这应该工作:

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字符串文字?

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"))

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. 最后一行的nextLine()条带。 If you want to add a line separator you can do 如果要添加行分隔符,可以执行

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.

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. 使用某些第三方库的能力是好的,但是对于学习的人来说最好是了解该理论,因为并非每个问题都存在某个第三方库。

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: 无论如何,这是起作用的:

content.replaceAll("(\\\\r)?\\\\n", System.getProperty("line.separator"));

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. 除非您实际上是在控制台中键入\\和r以及\\和n,否则根本不需要这样做:相反,您会产生重大误解。 The CR character is represented in a String as \\r but it consists of only one byte with the hex value 0xD. CR字符在字符串中表示为\\ r,但它仅包含一个十六进制值为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. 只需按Enter键:这就是它的作用。 It will transmit the CR byte into your code. 它将CR字节传输到您的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM