在分隔文本文件中搜索字符串

发布于 2024-12-26 20:20:48 字数 493 浏览 4 评论 0原文

假设我有一个字符串=“hello”。如何打开一个文本文件并检查该文本文件中是否存在 hello?

文本文件的内容:

hello:man:yeah

我尝试使用下面的代码。文件读取器只读取第一行吗?我需要它检查所有行以查看 hello 是否存在,然后如果存在,则从中取出“man”。

try {
    BufferedReader in = new BufferedReader(new FileReader("hello.txt"));
    String str;
    while ((str = in.readLine()) != null) {
        System.out.println(str);
    }
} catch (IOException e) {
    System.out.println("Error.");
}

Lets say I have a string = "hello". how do i open a text file and check if hello exists in that text file?

contents of the text file:

hello:man:yeah

i tried using the code below. Is it file reader only reads the first line? i need it to check all lines to see if hello exists, and then if it does, take "man" from it.

try {
    BufferedReader in = new BufferedReader(new FileReader("hello.txt"));
    String str;
    while ((str = in.readLine()) != null) {
        System.out.println(str);
    }
} catch (IOException e) {
    System.out.println("Error.");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一生独一 2025-01-02 20:20:48

如果 hello:man:yeah 是文件中的一行,那么您的代码工作正常。 readLine() 将读取一行,直到找到换行符(在本例中为一行)。

如果您只想查看它是否在文件中,那么您可以执行以下操作:

 String str;
 boolean found = false;
 while ((str = in.readLine()) != null) {
       if(str != null && !found){
         found = str.contains("hello") ? true : false;
       }
    }

如果您需要进行整个单词搜索,则需要使用正则表达式。用 \b 包围搜索文本将进行整个单词搜索。这是一个片段(注意,StringUtils 来自 Apache Commons Lang):

    List<String> tokens = new ArrayList<String>();
    tokens.add("hello");

    String patternString = "\\b(" + StringUtils.join(tokens, "|") + ")\\b";
    Pattern pattern = Pattern.compile(patternString);
    Matcher matcher = pattern.matcher(text);

    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }

当然,如果你没有多个令牌,你可以这样做:

String patternString = "\\bhello\\b";

If hello:man:yeah is one line in your file, then your code is working right. readLine() will read a line until a newline is found (one line in this case).

If you just want to see if it's in the file, then you could do something like this:

 String str;
 boolean found = false;
 while ((str = in.readLine()) != null) {
       if(str != null && !found){
         found = str.contains("hello") ? true : false;
       }
    }

If you need to do a whole word search, you'll need to use a regular expression. Surrounding your search text with \b will do the whole word search. Here's a snippet (Note, StringUtils comes from Apache Commons Lang):

    List<String> tokens = new ArrayList<String>();
    tokens.add("hello");

    String patternString = "\\b(" + StringUtils.join(tokens, "|") + ")\\b";
    Pattern pattern = Pattern.compile(patternString);
    Matcher matcher = pattern.matcher(text);

    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }

Of course, if you don't have multiple tokens, you can just do this:

String patternString = "\\bhello\\b";
没企图 2025-01-02 20:20:48

使用 String.indexOf()String.contains() 方法。

Use String.indexOf() or String.contains() method.

故笙诉离歌 2025-01-02 20:20:48

在每一行上使用 String.contains 方法。每一行都在 while 循环中处理。

Use the String.contains method on each line. Each line is processed in the while loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文