0

嗨,我有一个这样的字符串

"81.213.224.141","81.213.224.141","UAE","United Emirates","D3","Dubai","WildWadi","","32.100000","3.200100","Telecom","Eutelsat - SanComm". 我目前正在做的是只获取引号之间的数据,同时避免逗号并将它们存储在不同的字符串中。所以字符串 1 有第一个 IP,字符串 2 有第二个 IP,字符串 3 有国家代码等等。

因为"Eutelsat - SanComm"我想将它分成两部分并将它们存储在两个字符串中。所以第一个字符串会有Eutelsat,第二个会有SanComm.

到目前为止,我已经成功地拆分了字符串,但是它不仅在引用之间获取数据,而且它没有完全查看Telecom,例如仅从整个信息中查看。

代码:

str_Info = TextBox1.Text.Split(" ") //str_Info is a string having the TextBox 
                                      Which in turn has the string shown above

    For Each str_get As String In str_Info

    Next
End Sub

我认为我的拆分不能满足我的要求。有什么建议么 ?

4

2 回答 2

2

我建议使用 Textfieldparser:

    Dim tfp As New TextFieldParser("data.txt")
    With tfp
        .HasFieldsEnclosedInQuotes = True
        .SetDelimiters(",")
    End With
    Dim fields = tfp.ReadFields
    For Each field In fields
        Debug.Print(field)
    Next

现在你可以对每个字段做你想做的事。如果您希望它的类型更强大,您可以使用www.filehelpers.net在那里您可以创建一个类,然后从您的输入中“反序列化”。

使用 Filehelper,它可能看起来像:

Private Class SatlistConverter
    Inherits ConverterBase

    Public Overrides Function StringToField(from As String) As Object
        Return Strings.Split([from], " - ").ToList
    End Function
End Class

<DelimitedRecord(",")>
Private Class TheRecord
    <FieldQuoted>
    Public IP1, IP2, ISOCountry, Country, Something, City, Region, SomethingElse, Lattitude, Longitude, Provider As String
    <FieldQuoted, FieldConverter(GetType(SatlistConverter))>
    Public Satlist As List(Of String)
End Class

    Private Sub foo()

    Dim e As New FileHelperEngine(Of TheRecord)
    Dim records = DirectCast(e.ReadFile("data.txt"), TheRecord())
    For Each r In records
        Debug.Print(r.IP1)
        Debug.Print(r.IP2)
        For Each s In r.Satlist
            Debug.Print(s)
        Next
    Next

End Sub

Filehelper 有更多可能,但作为一个快速启动它可能就足够了。

就个人而言,我喜欢获得更强类型的东西的想法(请注意,您可以使用内置转换器使“2”成为整数),而不仅仅是字符串数组。

编辑更新示例以显示 CustomConverter 的使用

于 2012-11-27T11:52:52.310 回答
1

这应该这样做

    Dim noQuotes = Textbox1.Text.Replace("""", "") ' Remove quotes
    Dim split = noQuotes.Split(",") ' Split on commas

    Dim result As New List(Of String)()

    For Each str_get As String In split
        Dim splitStr = str_get.Split("-") ' Perform secondary split on dash
        For Each str_split As String In splitStr
            result.Add(str_split.Trim()) ' Enter into result list
        Next
    Next

您的值列表将在result

于 2012-11-27T11:45:26.417 回答