在vb.net中将文本行的部分提取为多个变量

时间:2014-12-07 09:05:42

标签: vb.net string dictionary

我创建了一个程序,它使用字典对象读取TXT文件的行并填充2个列表框。这是一本双语词典。 在文件中,行具有以下结构:

key ^ value(第一语言中的单词和第二语言中的单词除以^分隔符)

e.g。 前初步^ 后来^ DOPO 你好^侨

代码工作得很好...... 问题是我现在需要在多语言字典中对其进行转换,因此结构将是

key ^ value1 ^ value2 ^ value3(4种语言的单词全部除以分隔符)

但是我无法使字典对象适应它。

你会如何解决这个问题? 基本上我有一堆由4个部分和分隔符组成的行,我想将所有4个部分存储在4个变量中: - 定义 - 1stlang - 2ndlang - 3rdlang

我目前使用的双语代码是:

Public Class aero_dictionary

Dim dict As New Dictionary(Of String, String)
Private Sub Form1_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Deactivate
    Me.Opacity = 0.6
End Sub
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Activated
    Me.Opacity = 1
End Sub
Private Sub button1_Click(sender As System.Object, _
                          e As System.EventArgs) _
                        Handles Button1.Click
    Using OFD As New OpenFileDialog
        With OFD
            .Filter = "Dict files (*.dict)|*.dict"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                LeggiDizionario(.FileName)
            End If
        End With
    End Using
End Sub

Private Sub LeggiDizionario(FullPathFileName As String)

    Dim lines As String() = IO.File.ReadAllLines(FullPathFileName)

    For Each line As String In lines
        Dim kv As KeyValuePair(Of String, String) = ToKeyValuePair(line)
        dict.Add(kv.Key, kv.Value)
        ListBox1.Items.Add(kv.Key)
    Next

End Sub

Public Function ToKeyValuePair(pair As String) _
                               As KeyValuePair(Of String, String)

    Dim two As String() = pair.Split("^")
    Return New KeyValuePair(Of String, String)(two(0), two(1))
End Function
Private Sub listbox1_Click(sender As Object, _
                                       e As System.EventArgs) _
                                     Handles ListBox1.Click
    Dim lst As ListBox = DirectCast(ListBox1, ListBox)
    TextBox2.Clear()
    TextBox2.SelectedText = dict(lst.SelectedItem)
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

您可以定义字典以使用字符串数组,然后在dict.Add时添加3个元素的数组(少于键)。

Dim dict As New Dictionary(Of String, String())