I want to search for a string using StringTokenizer but the string I'm looking for has a delimiter in it - Java

核能气质少年 提交于 2019-12-23 18:14:53

问题


I have an external file named quotes.txt and I'll show you some contents of the file:

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'.

I used this: StringTokenizer str = new StringTokenizer(line, " .'");

This is the code for the searching:

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";
}

Now, I want to search for strings "Everybody's" and "it's" in line 1 and 2 but it won't work since the apostrophe is one of the delimiters. If I remove that delimiter, then I won't be able to search for "involvement", "commitment", "involved" and "committed" in line 3.

What suitable code can I do with this problem? Please help and thanks.


回答1:


I would suggest using regular expressions (the Pattern class) rather than StringTokenizer for this. For example:

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";
}



回答2:


Tokenize by whitespace, then trim by the ' character.



来源:https://stackoverflow.com/questions/8813779/i-want-to-search-for-a-string-using-stringtokenizer-but-the-string-im-looking-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!