搜索和读取.txt文件中的文本 [英] Search and Read text from .txt file
本文介绍了搜索和读取.txt文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码从.txt文件读取文本.但是我不知道如何在文件内执行搜索,以及如何基于搜索来读取文本文件中的特定行.
Hi,
I am using following code to read text from a .txt file. But I donot know how to perform search within file and how to read a specific line in the text file, based on search.
Dim vrDisplay = My.Computer.FileSystem.ReadAllText(CurDir() & "\keys.txt")
MsgBox(vrDisplay)
例如,如果我想阅读包含单词"Start"的行,该怎么做.
谢谢
For example if I want to read the line that contains word "Start", how to do that.
Thanks
推荐答案
尝试这个简单的解决方案:
Try this simple solution:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'A file named keys.txt contains
' afcd b g f
'rgb thh h h
'h hg hg
' h(hg)
' hg()
' h()
' hg(hjnthbrggfgn)
'Start your are ******
'end no one can do that
Dim filedata = My.Computer.FileSystem.ReadAllText(CurDir() & "\keys.txt")
Dim sp() = filedata.Split(vbNewLine)
Dim IndexOfStart = Array.FindIndex(Of String)(sp, AddressOf CheckStr)
Dim linecontainigStart = sp(IndexOfStart)
End Sub
Function CheckStr(ByVal line) As Boolean
Dim tmp = InStr(line, "Start")
If tmp > 0 Then
Return True
End If
Return False
End Function
您甚至尝试过在 ^ ]其他人如何执行此操作?
我在那里看到了很多有希望的链接.如果仍然遇到问题,请尝试其中的一些方法.回来,有人会帮助您.
Did you even try to look in Google [^]for how other people do this?
I see quite a few promising links in there. Try some of those, if you are still stuck; come back and someone will help you.
这篇关于搜索和读取.txt文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文