如何在VB.NET中搜索文本文件以显示特定字符串 [英] How do I search a text file in VB.NET to bring up a specific string
问题描述
你好,
我需要搜索一个带有8位整数的文本文件,找到一个包含8位整数的整行。
我试过使用streamreader虽然无法得到我想要的结果。
任何帮助都将不胜感激。
我尝试过的:
明确选项
进口system.io
昏暗搜索为StreamReader
Dim filename,textline As String,数量,价格,gtin8 As Integer
filename =(newname.txt)
gtin8 = TextBox1.Text
search = New StreamReader(filename)
如果search.contains(gtin8)那么
ListBox1.Items.Add(textstring)
为了在文件中搜索字符串,您需要完全或以块的形式读取文件。 Chunks更难,因为你必须要知道你的匹配字符串可能是在边界上并分散在两个块上。
但最简单的方法就是读取整个文件:
私有 功能 FileContainsString(path 作为 字符串,匹配作为 String ) As Boolean
Dim s As String = File.ReadAllText(path)
返回 s.Contains(匹配)
结束 功能跨度>
Hello,
I need to search a text file with an eight digit integer, to find a whole line which the eight digit integer is contained in.
I tried using streamreader though couldn't get the results I wanted.
Any help would be greatly appreciated.
What I have tried:
option explicit on
imports system.io
Dim search As StreamReader
Dim filename, textline As String, quantity, price, gtin8 As Integer
filename = ("newname.txt")
gtin8 = TextBox1.Text
search = New StreamReader(filename)
If search.contains(gtin8) Then
ListBox1.Items.Add(textstring)
In order to search the file for a string, you need to read the file, either completely, or in chunks. Chunks is harder, because you have to be aware that your match string could be "on the border" and spread across two chunks.
But the simplest way is just to read the whole file:
Private Function FileContainsString(path As String, match As String) As Boolean Dim s As String = File.ReadAllText(path) Return s.Contains(match) End Function
这篇关于如何在VB.NET中搜索文本文件以显示特定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!