Count How many lines in a Java String
- by Simon Guo
Need some compact code for counting how many lines in a string. It should be Java Code. And the string is separated by \r or \n will be considered as a separate line. For example,
"Hello\nWorld\nThis\nIs\t"
should return 4. The prototype is
private static int countLines(String str) {...}
Can someone provide a compact code? I have solution at here but it is too long, I think. Thank you.