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.

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
Ramya Selvarani
  • 499
  • 1
  • 9
  • 23
  • try `System.out.println(test.replaceAll("\n",",").replaceAll("\r\n",",")); ` – Suresh Atta Mar 03 '17 at 07:18
  • 2
    _"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 yourself – Jim Garrison Mar 03 '17 at 07:23

5 Answers5

20

\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(),","));
anacron
  • 6,443
  • 2
  • 26
  • 31
  • 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 working – Ramya Selvarani Mar 03 '17 at 07:08
  • please check the value of that string when you get from user through textArea. – santosh gore Mar 03 '17 at 07:10
  • Don't use \n but the a line separator as defined in http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html – Aubin Mar 03 '17 at 07:11
  • You can use the `System.lineSeparator();` instead of the `\n` character. – anacron Mar 03 '17 at 07:17
  • 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. – TNT Mar 03 '17 at 07:32
  • 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. – Aubin Mar 03 '17 at 14:08
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]+

Aubin
  • 14,617
  • 9
  • 61
  • 84
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 '\\'.

Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66
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

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
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.stack.imgur.com/nAv8d.png

Mateusz
  • 11
  • 2