【发布时间】:2018-09-03 09:22:03
【问题描述】:
所以我想搜索用户输入的参数,例如“Hello there”,而不是寻找“Hello”和“There”本身。
String[] words = text.split("\\s+");
for (int i = 0; i < words.length; i++){
if (searchString.contains(words[i])){
counter++;
}
}
这里的代码我基本上试图计算一个字符串在 txt 文件中出现的次数。
【问题讨论】:
-
嗯? “你好”是两个词。你为什么
spliting 文本?text.contains(searchString)?您要计算searchString在text中出现的次数吗? -
你的文本文件是什么样子的?请显示示例输入和预期输出。
-
是的,我想计算“Hello there”被放在一起的次数,而不是单独通过文本。我从空格中分离出来,是的,我想计算 searchString 出现了多少次。
-
这是预期的输出(java TextSearch "drops of blood" input.txt) 输出:雪,她刺破了手指,从雪上掉下三滴血。当她看到它看起来多么明亮和红色时,她总共匹配:1