如何从文本文件vb.net搜索和显示特定行 [英] how to search and display specific line from a text file vb.net

查看:74
本文介绍了如何从文本文件vb.net搜索和显示特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试搜索包含用户在文本框中输入内容的行,并显示整行.单击按钮后,下面的代码未显示消息框,我不确定是否已找到记录

Hi I am trying to search for a line which contains whats the user inputs in a text box and display the whole line. My code below doesnt display a messsagebox after the button has been clicked and i am not sure if the record has been found

   Dim filename, sr As String
    filename = My.Application.Info.DirectoryPath + "\" + "mul.txt"

    Dim file As String()
    Dim i As Integer = 0
    file = IO.File.ReadAllLines(filename)
    Dim found As Boolean
    Dim linecontain As Char

    sr = txtsr.ToString



    For Each line As String In file
        If line.Contains(sr) Then
            found = True
            Exit For
        End If
        i += 1
        If found = True Then
            MsgBox(line(i))
        End If


    Next



End Sub

推荐答案

您应该在此处调用 ReadLines ,而不是 ReadAllLines .区别在于, ReadAllLines 首先将整个文件内容读取到数组中,然后再开始处理其中的任何内容,而 ReadLines 直到处理完之后才读取行前一个.如果您想随机访问整个文件或要多次处理数据,则 ReadAllLines 很好.如果要在某行满足某些条件时停止处理数据,则 ReadLines 很好.如果要查找包含某些文本的行,并且您有一个包含一百万行且第一行与之匹配的文件,则 ReadAllLines 将读取全部一百万行,而 ReadLines 只会读第一篇.

You should be calling ReadLines here rather than ReadAllLines. The difference is that ReadAllLines reads the entire file contents into an array first, before you can start processing any of it, while ReadLines doesn't read a line until you have processed the previous one. ReadAllLines is good if you want random access to the whole file or you want to process the data multiple times. ReadLines is good if you want to stop processing data when a line satisfies some criterion. If you're looking for a line that contains some text and you have a file with one million lines where the first line matches, ReadAllLines would read all one millions lines whereas ReadLines would only read the first.

因此,这是显示包含特定文本的第一行的方法:

So, here's how you display the first line that contains specific text:

For Each line In File.ReadLines(filePath)
    If line.Contains(substring) Then
        MessageBox.Show(line)
        Exit For
    End If
Next

关于您的原始代码,您对 i 的使用是没有意义的.您似乎将 i 用作行计数器,但没有意义,因为您使用的是 For Each 循环,因此 line 包含该行.如果已经有了该行,为什么还要按索引获取该行?另外,当您尝试显示消息时,您正在使用 i line 编制索引,这意味着您将从行中获取单个字符,而不是数组中的单行.如果该行的索引大于该行中的字符数,则将抛出 IndexOutOfRangeException ,我猜这是您的情况.

With regards to your original code, your use of i makes no sense. You seem to be using i as a line counter but there's no point because you're using a For Each loop so line contains the line. If you already have the line, why would you need to get the line by index? Also, when you try to display the message, you are using i to index line, which means that you're going to get a single character from the line rather than a single line from the array. If the index of the line is greater than the number of characters in the line then that is going to throw an IndexOutOfRangeException, which I'm guessing is what's happening to you.

这是编写代码时首先不知道实际要做的事情的结果.如果您在编写代码之前就已经写出算法,那么很明显代码没有实现该算法.如果没有算法,则没有什么可以比较代码以确保它有意义.

This is what comes from writing code without knowing what it actually has to do first. If you had written out an algorithm before writing the code, it would have been obvious that the code didn't implement the algorithm. If you have no algorithm though, you have nothing to compare your code to to make sure that it makes sense.

这篇关于如何从文本文件vb.net搜索和显示特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆