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);
}