-1

我正在使用 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
   Dim fileReader As String
   fileReader = My.Computer.FileSystem.ReadAllText("C:\Users\user\Documents\text.txt")
   txt2.Text = (fileReader)
End Sub

谢谢

4

2 回答 2

1

因为您可以使用 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

[*]: 不对

于 2013-09-12T14:51:41.957 回答
1

正则表达式这是一个快速的解决方案,(\*)([\s\S]+)(\#)你可以找到任何介于*和之间的模式#

   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()
于 2013-09-12T15:14:50.780 回答