Dim reader As StreamReader = New StreamReader("C:\Users\S160358\Desktop\file.txt")
Do While reader.Peek() > -1
Dim line As String = reader.ReadLine()
If line.EndsWith("PSTAT") Then
Console.WriteLine("Yes")
Console.ReadLine()
Else
Console.WriteLine("No")
Console.ReadLine()
End If
Loop
Sample data
1111111111|22222222222222|3333333333|PSTAT
2222222222|33333333333333|
1111111111|PSTAT
AAAAAAAAAA|DDDDDDDDDDDDDD|FFFFFFFFFF|PSTAT
我正在尝试检查是否有任何行没有以某些字符结尾。如果返回“否”,则它将处理重新格式化功能。运行此代码后,它将返回“是”,并且它应返回“否”,因为第二行未以PSTAT结束。
答案 0 :(得分:2)
如果要检查所有行,那么在检查完所有行之前无法确定结果(或找到没有结尾的行):
Dim reader As StreamReader = New StreamReader("C:\Users\S160358\Desktop\file.txt")
Dim anyNo As Boolean = False
Do While reader.Peek() > -1
Dim line As String = reader.ReadLine()
If Not line.EndsWith("PSTAT") Then
anyNo = True
Exit Do
End If
Loop
If anyNo Then
Console.WriteLine("No")
Else
Console.WriteLine("Yes")
End If
Console.ReadLine()
或者简单地说:
Dim anyNo As Boolean = _
File.ReadAllLines("C:\Users\S160358\Desktop\file.txt") _
.Any(Function(line) Not line.EndsWith("PSTAT"))
If anyNo Then
Console.WriteLine("No")
Else
Console.WriteLine("Yes")
End If
Console.ReadLine()