在循环遍历StringBuffer时,我需要获取新行的起始位置.假设我在stringbuffer中有以下文档
"This is a test
Test
Testing Testing"
Run Code Online (Sandbox Code Playgroud)
在"测试","测试"和"测试"之后存在新行.
我需要这样的东西:
for(int i =0;i < StringBuffer.capacity(); i++){
if(StringBuffer.chatAt(i) == '\n')
System.out.println("New line at " + i);
}
Run Code Online (Sandbox Code Playgroud)
我知道这不起作用,因为'\n'不是一个角色.有任何想法吗?:)
谢谢
您可以这样简化循环:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19321 次 |
| 最近记录: |