I need to get the starting position of new line when looping through a StringBuffer. Say I have the following document in a stringbuffer
"This is a test
Test
Testing Testing"
New lines exist after "test", "Test" and "Testing".
I need something like:
for(int i =0;i < StringBuffer.capacity(); i++){
if(StringBuffer.chatAt(i) == '\n')
System.out.println("New line at " + i);
}
I know that won't work because '\n' isn't a character. Any ideas? :)
Thanks
You can simplify your loop as such:
StringBuffer str = new StringBuffer("This is a\ntest, this\n\nis a test\n");
for (int pos = str.indexOf("\n"); pos != -1; pos = str.indexOf("\n", pos + 1)) {
System.out.println("\\n at " + pos);
}
System.out.println("New line at " + stringBuffer.indexOf("\n"));
(no loop necessary anymore)
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