VB解析字符串并放入文本框

时间:2011-06-20 13:21:43

标签: .net vb.net

我正在尝试完成一项任务,但无法思考如何处理它。

以下是示例:

我有一个带有偏好的文本文件。我读了这个文件并在特定的行上找到了这样的文字:

 If InStr(sLine, "avidDirectory") Then

这是我在文本文件中的行:

 avidDirectory "S:\Avid MediaFiles\" "D:\Avid MediaFiles\" "Z:\Avid MediaFiles\"

我需要做的是读取quoations标记之间的每个字符串,并将每个字符串放在一个文本框中。

如果上面有5个不同的目录(上例中只有3个),我有5个文本框可供使用

所以我想我需要在引号之间捕获文本,从中创建一个新字符串,并将该字符串放入文本框

即;    string 1 = textbox1.txt 等

我该如何处理?

感谢。

1 个答案:

答案 0 :(得分:2)

创建一个新的Windows应用程序,并在您将要启动的表单中添加一个按钮和五个文本框,并用此代码替换表单的代码

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim txt As String = "avidDirectory ""S:\Avid MediaFiles\"" ""D:\Avid MediaFiles\"" ""Z:\Avid MediaFiles\"""
        Dim insideAQuotation As Boolean = False
        Dim array(5) As String
        Dim currentString As Integer = 0


        For i = 1 To Len(txt)
            If Mid(txt, i, 1) = Chr(34) And insideAQuotation Then
                insideAQuotation = False
                currentString += 1
            ElseIf Mid(txt, i, 1) = Chr(34) And insideAQuotation = False Then
                insideAQuotation = True
            End If

            If insideAQuotation Then
                If Mid(txt, i, 1) <> Chr(34) Then    'This is to avoid the quotation marks inside the text boxes.
                    array(currentString) &= Mid(txt, i, 1)
                End If
            End If
        Next

        Me.TextBox1.Text = array(0)
        Me.TextBox2.Text = array(1)
        Me.TextBox3.Text = array(2)
        Me.TextBox4.Text = array(3)
        Me.TextBox5.Text = array(4)



    End Sub
End Class