我有一个文件,其中部分内容如下:
bank: _nameless.292E.6438
player: _nameless.2843.0C10
companies: 312
这是我的vb.net文件,意图循环并给我以银行开头的行:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using sr As New StreamReader(currentfile)
Dim line As String
line = sr.ReadToEnd()
If line.StartsWith(" bank:") Then
Console.WriteLine(line)
End If
End Using
End Sub
但是,出于某种原因,这不起作用,并且不向控制台写任何内容。我在没有if语句的情况下尝试了它,它按预期写入整个文件。
bank:
直接从文本文件中复制并粘贴,因此这肯定是正确的文字。
提前致谢,Marcus
答案 0 :(得分:2)
按如下方式阅读每一行:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using sr As New StreamReader(currentfile)
While Not sr.EndOfStream()
Dim line As String = sr.ReadLine
If line.StartsWith(" bank:") Then
Console.WriteLine(line)
End If
End While
End Using
End Sub
答案 1 :(得分:1)
你可以像这样跳过空格,
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using sr As New StreamReader(currentfile)
While Not sr.EndOfStream()
Dim line As String = sr.ReadLine
line = line.Trim() 'This will remove any white space before and after in the line
If line.StartsWith("bank:") Then
Console.WriteLine(line)
End If
End While
End Using
End Sub
答案 2 :(得分:0)
使用此
如果是line.StartsWith(" bank:")那么 Console.WriteLine(行) 结束如果