如何拆分包含冒号分隔数据的文本文件

时间:2017-12-11 00:17:40

标签: vb.net

我正在使用VB创建一个项目,我正在尝试导入一个具有以下格式的行的文本文件: Email:Password

我已经设法逐行阅读,可以让我的程序显示个别行,例如MessageBox.Show(line(2))

我还设法拆分了这个文本文件,但它只填充数组Seperate(0)Seperate(1)

但是当我拨打Seperate(0)时,它会显示所有电子邮件,当我拨打Seperate(1)时,它会显示所有密码。

我想逐行拆分,因此我可以将值放在两个不同的文本框中(只需要1个电子邮件和密码)。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以先创建一个代表文件中1行的结构(电子邮件:密码),这只是一个自定义数据类型

Structure Account
    Public Email As String
    Public Pasword As String
End Structure

然后创建一个封装文件中电子邮件和密码解析的函数,此函数只需获取文件路径并返回它在文件中找到的所有电子邮件和密码。

它还处理重复项,因此它只添加唯一的。

Function GetAccounts(filePath As String) As Dictionary(Of String, String)
    'We need to a Dictionary data structure, so that we can easily avoid returning duplicates (if the file contains duplicates)
    Dim allaccounts As New Dictionary(Of String, String) 'Create the dictionary of accounts in the file (we will return this object)
    Dim fileInf As New FileInfo(filePath)
    fileInf.Refresh()
    If fileInf.Exists = True Then 'If file is found
        Dim sr As New StreamReader(fileInf.FullName) 'Load your file here
        Dim filecontent As String = sr.ReadToEnd 'Read the file content (Email:Password Lines)
        sr.Close() 'Close the stream on the file

        If String.IsNullOrEmpty(filecontent) = False Then 'Making sure that the file is not empty
            If InStr(filecontent, Chr(10)) <> 0 Then 'If the file has multiple lines, then we will need to loop through the lines
                Dim tmpSP() As String = filecontent.Split(Chr(10)) 'split the file content by lines and put the results in an array
                For i As Integer = 0 To tmpSP.Count - 1
                    'This will make sure that we read the line from the file without any TAB,NewLine characters..etc (clean line)
                    Dim tmp As String = tmpSP(i).Replace(Chr(13), "").Replace(Chr(11), "").Replace(Chr(9), "").Trim
                    If InStr(tmp, ":") <> 0 Then 'make sure that this line is in valid format (email:password)
                        Dim pair() As String = tmp.Split(":")
                        If IsNothing(pair) = False Then
                            If pair.Length = 2 Then
                                If allaccounts.ContainsKey(pair(0).ToLower) = False Then 'Email will be the Key, password will be the Value
                                    allaccounts.Add(pair(0).ToLower, pair(1).ToLower)
                                End If
                            End If
                        End If
                    End If
                Next
            Else 'If the file has only 1 line (1 email:password pair) then we take it
                If InStr(filecontent, ":") <> 0 Then 'making sure that this lonely line is in the correct (email:password format)
                    Dim tmp As String = filecontent.Replace(Chr(13), "").Replace(Chr(11), "").Replace(Chr(9), "").Trim
                    Dim pair() As String = tmp.Split(":")
                    If IsNothing(pair) = False Then
                        If pair.Length = 2 Then
                            If allaccounts.ContainsKey(pair(0).ToLower) = False Then 'Email will be the Key, password will be the Value
                                allaccounts.Add(pair(0).ToLower, pair(1).ToLower)
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End If

    Return allaccounts
End Function

要使用此功能,假设您要在button1.Click()

中解析该文件
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim foundaccounts As Dictionary(Of String, String)
    Dim ofd As New OpenFileDialog
    ofd.Filter = "Text File (*.txt)|*.txt"
    If ofd.ShowDialog = DialogResult.OK Then
        foundaccounts = GetAccounts(ofd.FileName)
        'You can now loop throught the list of returned emails:password in the file
        'Inside the below `For Each` & `Next` you can write Textbox1.Text = currentEmail, but not outside this below block
        For Each keyval As KeyValuePair(Of String, String) In foundaccounts
            Dim currentEmail As String = keyval.Key
            Dim currentPassword As String = keyval.Value
            Textbox1.Text = currentEmail
            Textbox2.Text = currentPassword
        Next        
    End If
End Sub

我尽可能多地发表评论,希望这能回答你的问题。

如果它没有或者有任何不清楚的地方,请告诉我。干杯:)