问:如果给我一个非常大的随机英文单词,并被告知找到由空格切割的特定子字符串[例如,“how now”、“brown milkow”等],然后返回它出现的位置,我该怎么做?
答:我有一个部分解决方案,但我请求 Stack Overflow 社区帮助完成最后一点。
程序如何运行:
- 为程序提供一个文本文件
- 搜索整个文件
返回行号和字号;字数与行相关
如果“how now”被发现为连续两行的前两个单词,它将返回在第 k 行位置 1 处找到的“how now”,并在第 k+1 行位置 1 处再次找到“how now”也是如此。
如果该行是“how now the count of montebrowncowcristo”,那么它应该能够将“hownow”和“browncow”检测为两个单独的事件。
解决方案 1:
int chn = 0;
int cbc = 0;
Scanner in = new Scanner(new File("filename.txt"));
String temp = in.nextLine();
Pattern phn = Pattern.compile("how now");
Pattern pbc = Pattern.compile("brown cow");
Matcher mhn = null;
Matcher mbc = null;
while (in.hasNext()) {
mhn = phn.matcher(temp);
while (mhn.find()) m++;
mbc = pbc.matcher(temp);
while (mbc.find()) j++;
temp = in.nextLine();
} // Formatted output comes after
问题是,虽然这通过使用模式和匹配器来跟踪出现次数(chn、cbc),并且还跟踪按时间顺序出现的次数,并且是这样做的最快算法,但我不知道如何我可以跟踪它发生在行中的位置。
解决方案 2:
Scanner in = new Scanner(new File("filename.txt"));
ArrayList<String> wordsInLine = new ArrayList<>();
String temp = in.nextLine();
String temp2 = "";
ctL = 1;
while (in.hasNext()) {
if (temp.contains("how now")) {
for (String word : temp.split(" ")) {
wordsInLine.add(word);
}
for (int i = 0; i < wordsInLine.size(); i++) {
if (wordsInLine.get(i).equals("how") ||
wordsInLine.get(i + 1).equals("now")) {
System.out.println("This returns line count and "
+ "the occurrence by getting i");
}
}
}
ctL++;
temp = in.nextLine();
}
但是第二个部分解决方案似乎效率低下且极其缓慢,对包含“现在如何”的每一行使用两个 for 循环。
有更优雅的方法吗?
最佳答案
关于Java:搜索文本中多次出现的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30657217/