如何从VB.NET中搜索特定的字符串到文本文件中 [英] How to search specific string into text file from VB.NET

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

问题描述

<pre><code></code>

推荐答案

你如何做到这一点将取决于许多因素:因为你没有试图详细解释,我会假设你什么都不知道,只是给你基础知识。



最简单的方法是将文本文件读入单个字符串,然后使用.NET框架查找匹配项:

How you do this will depend on a number of factors: since you haven't tried to explain in any detail, I'll assume you know nothing, and just give you the basics.

The easiest way to do this is to read the text file into a single string, and then use the .NET framework to find the match:
Dim text As String = File.ReadAllText("D:\Temp\MyFile.txt")
Dim index As Integer = text.IndexOf("hello")
If index >= 0 Then
   ' String is in file, starting at character "index"
End If



还有其他方法,但这几乎是最简单的。


There are other ways, but that is pretty much the simplest.


你需要StreamReader和Regx。



You need StreamReader and Regx for that.

//read file content in StreamReader
StreamReadertxt Reader = new StreamReader(fName);
szReadAll = txtReader.ReadToEnd();//Reads the whole text file to the end
txtReader.Close(); //Closes the text file after it is fully read.
txtReader = null;
//search word in file content
if (Regex.IsMatch(szReadAll, "SearchME", RegexOptions.IgnoreCase))//If the match is found in allRead
  MessageBox.Show("found");
else
  MessageBox.Show("not found");


为什么需要正则表达式?所有你需要的是一个streamreader





Dim Findstring = IO.File.ReadAllText(你的文件路径)

Dim Lookfor as string =hello



如果FindString.Contains(Lookfor)那么

Msgbox(Found:&寻找)

结束如果







虽然......你的方法会更清洁,可能更适合将来使用..你不需要正则表达式。
Why would you need Regex? All you need is a streamreader


Dim Findstring = IO.File.ReadAllText("Your File Path")
Dim Lookfor as string = "hello"

If FindString.Contains(Lookfor) then
Msgbox("Found: " & Lookfor)
end if



Although... Your method would be cleaner and probably better for future use.. You don't need Regex.


这篇关于如何从VB.NET中搜索特定的字符串到文本文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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