Is there a way to substring by the string line?
Someting like: String.substring(line_num);
Or we hava to do it manually by calculating the indexes?
No, that's not possible. You can, however, split the string by \n and then get the required line:
String s = "First line\nSecond line\nThird line\nFourth line";
String[] lines = s.split("\n", -1);
System.out.println(lines[2]); // Third line
If you don't know what the line separator of your platform is, use System.getProperty("line.separator").
Or if you want to get a range of lines, use Arrays.copyOfRange():
String s = "First line\nSecond line\nThird line\nFourth line";
String[] lines = s.split("\n", -1);
String[] lines2To4 = Arrays.copyOfRange(lines, 1, 4);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With