Get the number of lines in a file by counting the number of newline characters inside a String (and adding 1).
(String what)
| 39 | * characters inside a String (and adding 1). |
| 40 | */ |
| 41 | static public int countLines(String what) { |
| 42 | int count = 1; |
| 43 | for (char c : what.toCharArray()) { |
| 44 | if (c == '\n') count++; |
| 45 | } |
| 46 | return count; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /** |