简体   繁体   中英

how to split array data in vb.net?

I have a datatable that returns a column name location which is a combination of city,state and zipcode,i have stored that column values in an array. now i want to split the array data and store in another array. here is the code but it is not working.

Dim eTemp As DataTable = dt.DefaultView.ToTable(True, "Location")
Dim s() As String
Dim Count As Integer = PortCodeTemp.Rows.Count - 1
ReDim s(0 To Count)
For i = 0 To Count
    s(i) = PortCodeTemp.Rows(i).Item("Location")
    For t As Integer = 0 To Count
        Dim items As String() = s.Split(",".ToCharArray())
    Next
Next

Your issue is here

Dim items As String() = s.Split(",".ToCharArray())

because there is no Split method for an array.

I think you meant to split the string at index i , which is where you stored the string value of location.

Dim items As String() = s(i).Split(",".ToCharArray())

Update

I'm not sure why you're doing it this way but here is something you can try. Instead of using an array I just used a List(Of String) so there's no need to be doing a redim in every loop.

Dim allItems As New List(Of String)
For i = 0 To PortCodeTemp.Rows.Count - 1
    Dim location as String = PortCodeTemp.Rows(i).Item("Location")
    allItems.AddRange(location.Split(",".ToCharArray()))
Next

Therefore, allItems should contain everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM