Java String split String by \r\n new line and trim space

Description

Java String split String by \r\n new line and trim space

public class Main {
   public static void main(String[] args) {
      String unixLine = "Line 1 \n Line 2 \n Line 3";
      String[] lines = unixLine.split("\\r?\\n");
      for (String line : lines) {
         System.out.println(">"+line+"<");
      }//from  w  w w  . j a va2  s.  c om
      System.out.println();
      
      String windowLine = "Line 1 \r\n Line 2 \r\n Line 3";
      lines = windowLine.split("\\r?\\n");
      for (String line : lines) {
        System.out.println(">"+line+"<");
      }
      System.out.println();
      
      //trimming each element.
      lines = windowLine.split("\\s*\\r?\\n\\s*");
      for (String line : lines) {
        System.out.println(">"+line+"<");
      }
   }
}



PreviousNext

Related