Replace '\n' by ',' in java
Asked Answered
A

5

9

I want to take input from user as String and replace the newline character \n with ,

I tried :

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

Output was s1,s2,s3,s4

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

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

s1

s2

s3

s4

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

Please suggest what is wrong.

Amply answered 3/3, 2017 at 7:5 Comment(2)
try System.out.println(test.replaceAll("\n",",").replaceAll("\r\n",",")); Curiel
"getting input from UI " - Show the code that gets the input from the UI. Clearly whatever does the reading is not interpreting the escape code and leaving the literal \n in the string. You may need to interpret escape sequences yourselfMillur
O
21

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

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

Update:

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

System.out.println(test.replaceAll(System.lineSeparator(),","));
Olvan answered 3/3, 2017 at 7:6 Comment(6)
This is working fine when I give the string as hardcoded one. but when I get the string from user through textarea it's not workingAmply
please check the value of that string when you get from user through textArea.Compulsive
Don't use \n but the a line separator as defined in docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.htmlYork
You can use the System.lineSeparator(); instead of the \n character.Olvan
I think in order to get System.lineSeparator() to work, the regex string has to be formatted like a regex. So something like replaceAll("[" + System.lineSeparator() + "]+"). That's the only way I could get it to work on my device.Pry
System.lineSeparator() is dependent of the os where java is executed. In case of client/server activity like Web Development, you can't presume of the line terminator of the client with this method.York
Y
3

java.util.regex.Pattern documentation specifies Line terminators as :

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

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

regex for that is [\r\n]+

York answered 3/3, 2017 at 7:14 Comment(0)
M
1

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

The Java String after user input will look like

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 '\\'.

Mangle answered 3/3, 2017 at 7:14 Comment(3)
how can we replace \\n in this case?Alundum
@sony System.out.println(test.replaceAll("\\n",","));Mangle
Thank you.. @MarkusAlundum
S
1

Using 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

Sarene answered 3/3, 2017 at 7:26 Comment(0)
M
0

If someone will get from UI \\n in text and want remove one \ to get next line sign \n then can use this:

        String text = "text\\ntext\\ntext\\ntext";
    System.out.println(text.replaceAll("\\\\n", "\n"));

https://i.sstatic.net/nAv8d.png

Menstruate answered 22/2, 2022 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.