HShbib
HShbib

Reputation: 1841

Capturing parameters from a string represented in quotations and commas

Hi I have a string which is like this

"81.213.224.141","81.213.224.141","UAE","United Emirates","D3","Dubai","WildWadi","","32.100000","3.200100","Telecom","Eutelsat - SanComm". What I am currently working on is to only get the data between the quotations while avoiding commas and store them in different strings. So String 1 has the first IP, String 2 has the second IP, String 3 has country code and etc.

For "Eutelsat - SanComm" I would like to split it into two and store them in two strings. So first string would have Eutelsat and the second would have SanComm.

So far I have managed to split the string however it is not only taking the data between quotations as well as it is not viewing fully e.g. only Telecom out of the entire information.

Code:

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

I think my splitting is not satisfying my requirement. Any suggestions ?

Upvotes: 0

Views: 96

Answers (2)

igrimpe
igrimpe

Reputation: 1785

I'd suggest the 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

Now you can do what you want with each field. If you want it more strong typed, you could use www.filehelpers.net where you can create a class that is then "deserialized" from your input.

With Filehelper it could look like:

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

There is much more possible with Filehelper, but as a jump start it might be sufficient.

Personally I like the idea to get something more strongly typed (note that you can use built in converters to make "2" an integer for example) and not just an array of string.

EDIT Updated sample to show use of a CustomConverter

Upvotes: 2

Blachshma
Blachshma

Reputation: 17395

This should do it

    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

Your list of values will be in result

Upvotes: 1

Related Questions