在.txt文件中查找所有字符串"the"

Gif*_*ary 11 java

这是我的代码:

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

public class SearchThe {
    public static void main(String args[]) {
        try {
            String stringSearch = "the";
            // Open the file c:\test.txt as a buffered reader
            BufferedReader bf = new BufferedReader(new FileReader("test.txt"));

            // Start a line count and declare a string to hold our current line.
            int linecount = 0;
                String line;

            // Let the user know what we are searching for
            System.out.println("Searching for " + stringSearch + " in file...");

            // Loop through each line, stashing the line into our line variable.
            while (( line = bf.readLine()) != null){
                // Increment the count and find the index of the word
                linecount++;
                int indexfound = line.indexOf(stringSearch);

                // If greater than -1, means we found the word
                if (indexfound > -1) {
                    System.out.println("Word was found at position " + indexfound + " on line " + linecount);
                }
            }

            // Close the file after done searching
            bf.close();
        }
        catch (IOException e) {
            System.out.println("IO Error Occurred: " + e.toString());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在test.txt文件中找到一些单词"the".问题是当我找到第一个"the"时,我的程序停止找到更多.

当一些像"然后"这样的词时,我的程序将其理解为"the"这个词.

Cha*_*ick 15

使用正则表达式不区分大小写,使用单词边界查找"the"的所有实例和变体.

indexOf("the")无法辨别"the""then",因为每个都以"the"开头.同样,"the"位于"anathema"的中间.

要避免这种情况,请使用正则表达式,并搜索"the",并\b在任一侧使用单词boundary().使用单词边界,而不是在""上拆分,或者只使用indexOf(" the ")(两边的空格),这些都不会找到"the".和标点符号旁边的其他实例.您也可以对搜索案例不敏感地查找"The".

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 使用正则表达式+1,比其他'拆分'选项(包括我的)更好. (3认同)