Replace '\n' by ',' in java

38,121

Solution 1

\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(),","));

Solution 2

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]+

Solution 3

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

Solution 4

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

Share:
38,121

Related videos on Youtube

Ramya Selvarani
Author by

Ramya Selvarani

I have graduated from Dr.Mahalingam college of Engineering and Technology,Which is affiliated to Anna University.I had Information Technology as my major. My interested areas are web application development, Service Oriented Architecture, Enterprise Middleware and Web Services. Currently I am working at Marlabs Software pvt Ltd.

Updated on February 23, 2022

Comments

  • Ramya Selvarani
    Ramya Selvarani about 2 years

    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.

    • Suresh Atta
      Suresh Atta about 7 years
      try System.out.println(test.replaceAll("\n",",").replaceAll("\r\‌​n",","));
    • Jim Garrison
      Jim Garrison about 7 years
      "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
  • Ramya Selvarani
    Ramya Selvarani about 7 years
    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
  • santosh gore
    santosh gore about 7 years
    please check the value of that string when you get from user through textArea.
  • Aubin
    Aubin about 7 years
    Don't use \n but the a line separator as defined in docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.ht‌​ml
  • anacron
    anacron about 7 years
    You can use the System.lineSeparator(); instead of the \n character.
  • TNT
    TNT about 7 years
    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.
  • Aubin
    Aubin about 7 years
    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.
  • Admin
    Admin almost 5 years
    how can we replace \\n in this case?
  • Markus Lausberg
    Markus Lausberg almost 5 years
    @sony System.out.println(test.replaceAll("\\n",","));
  • Admin
    Admin almost 5 years
    Thank you.. @Markus