views:

124

answers:

2

Hello all,

I had a question regarding the split function used in Visual Basic. As of now I have written a function that brings in a string value. The string value returned will probably look like this "List1;List2; Field1,Field2". My goal is to use the split function for this string to put all the lists in one array and to put the fields in another array.

The problem is between where List2 and Field1 are. I don't want Field1 to be placed in the list array. I'm thinking there might be a way to do this by parsing but I'm not sure. Here is my current code below. Thanks in advance for any help.

        Dim s As String = GetSetting("ReOrderList", properties.SiteId)
        Dim affectedLists() As String = s.Split(";")
        Dim affectedFields() As String = s.Split(",")
A: 

Use String.LastIndexOf to find where to divide your string.

Imports System

Class Test
    Shared Sub Main()

     Dim sample As String = "List1;List2; Field1,Field2"

     Dim middle As Int32 = sample.LastIndexOf(";")

     Dim lists As String = sample.Substring(0, middle).Trim()
     Dim fields As String = sample.Substring(middle + 1).Trim()

     Dim affectedLists As String() = lists.Split(";"C)
     Dim affectedFields As String() = fields.Split(","C)

    End Sub
End Class
Andrew Hare
Thanks a lot everybody. I couldn't believe how fast I got an answer.
Andy
A: 

If you know that the lists will come before the fields, you can chop the string at the last ';' character:

Dim splitPos As Integer = input.LastIndexOf(";"c)
Dim lists As String = input.Substring(0, splitPos+1)
Dim fields As String = input.Substring(splitPos+1, input.Length - (splitPos+1))

Then you can work on the lists and fields separately.

Fredrik Mörk