VB - 如果StreamReader读取包含(“string1”)但缺少的行(“string2”)

时间:2015-05-12 17:39:25

标签: vb.net string contains

编辑: 如何检索符合上述条件的文件的文件名?

如何检查字符串是否包含“value1”但没有“value2”?我试过这个:

While Not sr.EndOfStream
    Dim sLine As String = sr.ReadLine
    If sLine.Contains("value1") Then
        If Not sLine.Contains("value2") Then
            sw.WriteLine("write name of file that meets conditions to txt file")
End While

不确定如何搜索缺失值。

2 个答案:

答案 0 :(得分:3)

您可以将整个文件加载到字符串中并执行检查

If wholeFileData.Contains("value1") AndAlso Not wholeFileData.Contains("value2") Then
    sw.WriteLine("write name of file that meets conditions to txt file")
End If

如果您需要循环每一行,那么您需要存储一个变量并在结尾显示该消息。

Dim containsValue1, containsValue2 As Boolean

containsValue1 = False
containsValue2 = False

While Not sr.EndOfStream
    Dim sLine As String = sr.ReadLine

    If sLine.Contains("value1") Then
        containsValue1 = True
    End If

    If sLine.Contains("value2") Then
        containsValue2 = True
    End If
End While

If containsValue1 AndAlso Not containsValue2 Then
    sw.WriteLine("write name of file that meets conditions to txt file")
End If

答案 1 :(得分:0)

If sLine.Contains("value1") = True and sLine.Contains("value2") = False Then
    Msgbox("sLine contains value1, but does not contain value2")
End If