在java中用','替换'\n'

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

Replace '\n' by ',' in java

javastringreplacereplaceall

提问by Ramya Selvarani

I want to take input from user as Stringand replace the newline character \nwith ,

我想利用来自用户的输入作为String和替换换行符\n,

I tried :

我试过 :

String test ="s1\ns2\ns3\ns4"; System.out.println(test.replaceAll("\n",","));

String test ="s1\ns2\ns3\ns4"; System.out.println(test.replaceAll("\n",","));

Output was s1,s2,s3,s4

输出为 s1,s2,s3,s4

But when I try the same code by getting input from UI it's not working.

但是当我通过从 UI 获取输入来尝试相同的代码时,它不起作用。

When I debug it the string test(which I hardcoded) is treated as,

当我调试它时,字符串测试(我硬编码)被视为,

s1

s1

s2

s2

s3

s3

s4

s4

but the string from UI is "s1\ns2\ns3\ns4".

但是来自 UI 的字符串是“ s1\ns2\ns3\ns4”。

Please suggest what is wrong.

请提出什么问题。

采纳答案by anacron

\nis the new line character. If you need to replace that actual backslash character followed by n, Then you need to use this:

\n是换行符。如果您需要替换后跟的实际反斜杠字符n,那么您需要使用以下命令:

String test ="s1\ns2\ns3\ns4";
System.out.println(test.replaceAll("\n",","));

Update:

更新:

You can use the System.lineSeparator();instead of the \ncharacter.

您可以使用System.lineSeparator();代替\n字符。

System.out.println(test.replaceAll(System.lineSeparator(),","));

回答by Markus Lausberg

As anacron already pointed out '\n' is a special charachter and different to the user input "\n", because the user input is transformed to "\\n".

正如 anacron 已经指出的 '\n' 是一个特殊字符,与用户输入“\n”不同,因为用户输入被转换为“\\n”。

The Java String after user input will look like

用户输入后的 Java 字符串看起来像

String test ="s1\ns2\ns3\ns4";

and not like your test string

而不喜欢你的测试字符串

String test ="s1\ns2\ns3\ns4";

The user will input single charachter and the keyboard '\' is transformed to Java charachter '\\'.

用户将输入单个字符,键盘“\”将转换为 Java 字符“\\”。

回答by Aubin

java.util.regex.Pattern documentationspecifies Line terminators as :

java.util.regex.Pattern文档将行终止符指定为:

A line terminator is a one- or two-character sequence that marks the end of a line of the input character sequence. The following are recognized as line terminators:

A newline (line feed) character ('\n'), A carriage-return character followed immediately by a newline character ("\r\n"), A standalone carriage-return character ('\r'), A next-line character ('\u0085'), A line-separator character ('\u2028'), or A paragraph-separator character ('\u2029).

行终止符是一个或两个字符的序列,用于标记输入字符序列的一行的结束。以下被识别为行终止符:

一个换行符(换行符)('\n'),一个回车符紧跟一个换行符(“\r\n”),一个独立的回车符('\r'),一个下一个-行字符 ('\u0085')、行分隔符 ('\u2028') 或段落分隔符 ('\u2029)。

Your line terminator, from textarea, are \r\n (CR/LF).

来自 textarea 的行终止符是 \r\n (CR/LF)。

regex for that is [\r\n]+

正则表达式是 [\r\n]+

回答by Mehraj Malik

Using Regex:

使用Regex

public class Program
{
    public static void main(String[] args) {
        String str = "s1\ns2\ns3\ns4";
        str = str.replaceAll("(\r\n|\n)", ",");
        System.out.println(str);
    }
}

outout : s1,s2,s3,s4

输出:s1,s2,s3,s4