如何在VB.NET中搜索文本文件以显示特定字符​​串 [英] How do I search a text file in VB.NET to bring up a specific string

查看:126
本文介绍了如何在VB.NET中搜索文本文件以显示特定字符​​串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我需要搜索一个带有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屋!

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