假设我有结构如下的字符串
This is sentence one.
this is sentence two.
This is sentence three.
This is sentence 4.
This is sentence 5.
This is sentence 6.
我的要求是在遇到两个空行后获取文本。
我只想要这个 这是第5句。 这是第 6 句。
如何在 JAVA 中实现此目的?
最佳答案
这是一种简单而丑陋的方法:
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(new FileReader("input.txt"));
String empty = "";
int counter = 0;
while (in.hasNextLine()) {
String temp = in.nextLine();
if (temp.equalsIgnoreCase(empty)) {
counter++;
if (counter == 2) {
System.out.println(in.nextLine());
System.out.println(in.nextLine());
System.exit(1);
}
}
}
}
感谢用户 mbroshi 指出我的旧代码有缺陷
关于java - Java中如何获取两个空行后的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21997276/