我有一个名为quotes.txt 的外部文件,我将向您展示该文件的一些内容:
1 Everybody's always telling me one thing and out the other.
2 I love criticism just so long as it's unqualified praise.
3 The difference between 'involvement' and 'commitment' is like an eggs-and-ham
breakfast: the chicken was 'involved' - the pig was 'committed'.
我用过这个:StringTokenizer str = new StringTokenizer(line, " .'");
这是搜索的代码:
String line = "";
boolean wordFound = false;
while((line = bufRead.readLine()) != null) {
while(str.hasMoreTokens()) {
String next = str.nextToken();
if(next.equalsIgnoreCase(targetWord) {
wordFound = true;
output = line;
break;
}
}
if(wordFound) break;
else output = "Quote not found";
}
现在,我想搜索字符串"Everybody's"
and "it's"
在第 1 行和第 2 行中,但它不起作用,因为撇号是分隔符之一。如果我删除该分隔符,那么我将无法搜索"involvement"
, "commitment"
, "involved"
and "committed"
在第 3 行。
我可以用什么合适的代码来解决这个问题?请帮忙并表示感谢。
我建议使用正则表达式(the Pattern class http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html) 而不是StringTokenizer
为了这。例如:
final Pattern targetWordPattern =
Pattern.compile("\\b" + Pattern.quote(targetWord) + "\\b",
Pattern.CASE_INSENSITIVE);
String line = "";
boolean wordFound = false;
while((line = bufRead.readLine()) != null) {
if(targetWordPattern.matcher(line).find()) {
wordFound = true;
break;
}
else
output = "Quote not found";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)