Count the number of lines in a Java String -


In Java there is a need for some compact code to count the number of rows in a string string \ r or \ n . Each of these new characters will be treated as a separate line. For example,

  "Hello \ nWorld \ nIt \ nIs \ t"  

should come back 4.The prototype is

< Pre> Private Static Entline Line (String Straw) {...}

Can someone provide a compact set of statements? I have a solution here but it's too long, I think. Thank you

  private static int countLines (string str) {string [] lines = str .split ( "\ R \ n | \ r | \ n"); Return lines. Length; }  

Comments