Thursday, April 7, 2016

How to remove line separators from a string in java

Line separators like new line (\n) and carriage return (\r) can be removed by using the regex \\p{Cntrl}.

Example:

//How to remove line separators in string

package com.test;
public class Test {

 public static void main(String[] args) {
  String s="testing new line \n and carriage return \r with regex";

  System.out.println(s);
  String st=s.replaceAll("\\p{Cntrl}", "");
  System.out.println(st);
 }

}

No comments:

Post a Comment