Find all string “the” in .txt file

后端 未结 5 2064
后悔当初
后悔当初 2020-12-31 09:28

Here is my code:

// Import io so we can use file objects
import java.io.*;

public class SearchThe {
    public static void main(String args[]) {
        try         


        
5条回答
  •  孤独总比滥情好
    2020-12-31 10:16

    Use Regexes case insensitively, with word boundaries to find all instances and variations of "the".

    indexOf("the") can not discern between "the" and "then" since each starts with "the". Likewise, "the" is found in the middle of "anathema".

    To avoid this, use regexes, and search for "the", with word boundaries (\b) on either side. Use word boundaries, instead of splitting on " ", or using just indexOf(" the ") (spaces on either side) which would not find "the." and other instances next to punctuation. You can also do your search case insensitively to find "The" as well.

    Pattern p = Pattern.compile("\\bthe\\b", Pattern.CASE_INSENSITIVE);
    
    while ( (line = bf.readLine()) != null) {
        linecount++;
    
        Matcher m = p.matcher(line);
    
        // indicate all matches on the line
        while (m.find()) {
            System.out.println("Word was found at position " + 
                           m.start() + " on line " + linecount);
        }
    }
    

提交回复
热议问题