将字符串解析为数组(VB)

时间:2018-09-27 18:06:21

标签: vb.net string-parsing

我试图制作一个程序,该程序将从Chrome:// policy(由RichTextbox输入)中的原始列表解析为文本数组,然后将其转储到另一个RichTextbox中。所有原始字符串的长度正好为32个字符,后跟一个逗号。这是代码:

Public Class Form1
    Dim tempExt As String
    Dim extsHandled As Integer
    Dim numOfExts As Integer
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If RichTextBox1.Text = "" Then
            MsgBox("Please enter the raw extensions from Chrome://policy")
        Else
            RichTextBox3.Text = RichTextBox1.Text
            numOfExts = TextBox1.Text
            Dim place As Integer = 0
            Dim exts(150) As String
            While extsHandled < numOfExts
                tempExt = RichTextBox1.Text.Substring(0, 32)
                exts(place) = tempExt
                RichTextBox1.Text.Remove(0, 33)
                place = place + 1
                extsHandled = extsHandled + 1
            End While
            Dim newPlace As Integer = 0
            While newPlace < numOfExts
                RichTextBox2.AppendText(exts(newPlace))
                newPlace = newPlace + 1
                RichTextBox2.AppendText(" ")
            End While

        End If
    End Sub
End Class

大多数方法都有效,但是从richtextbox中删除字符似乎出了问题,因为当我运行它时,它只会一遍又一遍地解析字符串的第一部分:

enter image description here

我做错什么了吗?

2 个答案:

答案 0 :(得分:1)

如果总是那样,您可以这样做:

RichTextBox3.Text = RichTextBox1.Text.Replace(",", vbNewLine)

#3是您的结果,而#1是原始的权利吗?

enter image description here

是的,您可以简单地计算出其中的数量

RichTextBox2.Text=  RichTextBox1.Text.Split({","}, StringSplitOptions.RemoveEmptyEntries).Count.ToString

答案 1 :(得分:0)

此行返回一个新字符串:

RichTextBox1.Text.Remove(0, 33)

它不会修改文本框。在循环的下一次迭代中,您仍在使用原始值,在字符串的开头查看相同的32个字符集。

此外,此代码中没有任何内容可初始化extsHandled变量。您应该打开Option Strict,这有助于捕获此类错误。严格禁用Option是不明智的做法。您还应该给您将实际从代码中引用的任何控件赋予有意义的名称。

目前我尚不清楚确切的格式。如果它们全部在同一行上(即使换行也没有换行符作为字符串的一部分),这应该可以工作:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If String.IsNullOrWhitespace(RichTextBox1.Text) Then
        MsgBox("Please enter the raw extensions from Chrome://policy")
        Exit Sub
    End If

    RichTextBox3.Text = RichTextBox1.Text
    Dim exts() As String
    Using rdr As New TextFieldParser(RichTextBox1.Text)
        rdr.TextFieldType = FileIO.FieldType.Delimited
        rdr.Delimiters = New String() {","}
        exts = rdr.ReadFields()
    End Using

    For Each ext As String In exts
         RichTextBox2.AppendText(ext)
    Next ext

    RichTextBox1.Text = ""
End Sub

问题是此代码不执行任何操作。方法结束时,数组消失了。考虑将数组作为类的属性,或使用将数组作为结果返回的方法。

您也可以查看此内容,以免在文本框中输入内容,尽管这只是一个起点:

Public Function GetChromeExtensionKeys() As IEnumerable(Of String)
    Dim BasePath As String = 
EndEnvironment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
    BasePath = Path.Combine(BasePath, "Google\Chrome\User Data")

    Dim dflt As String = Path.Combine(BasePath, "Default\Extensions")
    Dim profileExts() As String = Directory.GetDirectories(BasePath, "Profile *").
          Select(Function(p) Path.Combine(p, "Extensions"))

    Dim result As New List(Of String)()
    result.AddRange(Directory.GetDirectories(dflt))
    For Each folder As String In profiles
        result.AddRange(Directory.GetDirectories(folder))
    Next folder   

    Return result.Select(Function(e) Path.GetFileName(e)).Distinct()
Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each ext As String In GetChromeExtensionKeys()
         RichTextBox2.AppendText(ext)
    Next ext
End Sub