为valye逐行读取逗号分隔字符串

时间:2017-01-20 20:54:04

标签: vb.net

我有以下代码,它接受文件的加密内容并将其解密为字符串。这只是一个逗号分隔的字符串,如下所示:

加里,001,0006

Ben,002,00

我想检查第0列和第1列中的值是否匹配文本框的内容,但它必须在同一行上,因此我需要读取每一行/每行。我已经完成了大部分工作,我只是不知道如何逐行读取字符串并检查它的列,有人可以帮忙吗?

我评论的代码只是我正在尝试做的伪代码。

Using TextReader As New IO.StreamReader("C:\Users\Garry\Desktop\test.txt")
    Dim EncryptedString As String = TextReader.ReadToEnd
    Dim DecryptedString As String = AESD(EncryptedString, Password)

    Dim strReader As New StringReader(DecryptedString)

    Dim line As String = strReader.ReadLine

    'If Line.column(0).contains(txtName.text) AND Line.column(1).contains(txtEnteredKey.text) then 

    'Else
    'Go to next line
    'End If

End Using

2 个答案:

答案 0 :(得分:1)

您可以按,拆分字符串,然后将列与您想要的值进行比较。例如,如果我想在列表中找到Ben,我可以这样做:

Dim s As String = "Garry, 001, 0006
Ben, 002, 00"

Dim strReader As New StringReader(s)

Dim line = strReader.ReadLine
While String.IsNullOrEmpty(line) = False
    Dim parts = line.Split(","c)
    If (parts(0).Trim() = "Ben" And parts(1).Trim() = "002") Then
        Console.WriteLine("Found ben!")
        Exit While
    End If
    line = strReader.ReadLine
End While
Console.ReadLine()

答案 1 :(得分:1)

你也可以这样做:

    Dim DecryptedString As String = AESD(System.IO.File.ReadAllText("C:\Users\Garry\Desktop\test.txt"), Password)

    Dim values() As String
    For Each line As String In DecryptedString.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
        values = line.Split(",")
        If values.Length >= 2 AndAlso values(0) = txtName.Text AndAlso values(1) = txtEnteredKey.Text Then
            ' ... do something ...
            Exit For
        End If
    Next
相关问题