如何使用vb.net读取txt文件中的某一行?
我正在使用vb.net,这是我的test.txt文件中的内容:如何使用vb.net读取txt文件中的某一行?,vb.net,Vb.net,我正在使用vb.net,这是我的test.txt文件中的内容: My name is james I was born in Melaka * I like to eat fish # My hobby is riding 我想让我的函数读取符号*和# 这是我的函数,我将如何编辑它以将条件放入 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
My name is james
I was born in Melaka
*
I like to eat fish
#
My hobby is riding
我想让我的函数读取符号*和#
这是我的函数,我将如何编辑它以将条件放入
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\Users\user\Documents\text.txt")
txt2.Text = (fileReader)
End Sub
谢谢你,因为你可以用LINQ做任何事:
Dim result = From line In File.ReadLines("c:\path\to\your\file.txt")
Skip While line <> "*"
Skip 1
Take While line <> "#"
For Each line In result
' Do something '
Next
Dim result=From-In-File.ReadLines(“c:\path\to\your\File.txt”)中的行
跳过第行“*”
跳过1
“边拍边拍”行“#”
对于结果中的每一行
“做点什么”
下一个
[*]:不正确正则表达式这是一个快速的解决方案,使用
(\*)([\s\s]+)(\\\\\\\)
模式,您可以找到*
和\\\\
我在您的代码中没有看到任何试图定位或读取您试图获取的部分的内容。你忘了那部分代码了吗?我已经编辑了我的问题。对不起,我的哑巴,你试过什么了?正如Ken所说,您甚至没有尝试在字符串中搜索*和#符号。你必须付出努力才能得到任何帮助。我会使用
ReadAllLines
方法而不是realltext
,因为它返回一个字符串数组,这将使你更容易在其中搜索。嗯,不。添加一个添加“如何编辑它以放入条件?”的句子并不是一种努力。你在这个网站上搜索过以前关于[vb.net]查找字符串文本
之类的问题吗?在一些简单的任务中,linq不是最好的选择
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\Users\user\Documents\text.txt")
txt2.Text = System.Text.RegularExpressions.Regex.Match(fileReader,"(\*)([\s\S]+)(\#)").toString()