views:

379

answers:

1

Using VB.NET - I have a string:

"##RES00012##Some value ##RES00034##Another value"

That I want to split using the "##RES" as a seperator to:

"##RES00012## Some value " and "##RES00034## Another value"

The string.split function doesn't seem to offer an overload to split on multiple characters or array of characters and maintain the seperator, which is required for functional purposes.

I'm looking at simply searching for indexOf("##res") and using string manipulation to do this unless I'm missing something obvious? I've searched SO for a solution but unable to find anything that actually does what I'm after.

The following is the closest i've found: how-do-i-split-a-string-by-a-multi-character-delimiter-in-c

+3  A: 

Splitting on multiple characters is not that tricky; there are overloads on the String.Split method that does that:

Dim input As String = "##RES00012## Some value ##RES00034## Another value"
Dim parts As String() = input.Split(New String() {"##RES"}, StringSplitOptions.RemoveEmptyEntries)

This will give you an array with two elements:

"00012## Some value "
"00034## Another value"

However, the separator is left out. This is not overly tricky though; it should be prepended to each of the elements (except the first one if the string does not start with the separator):

Dim input As String = "##RES00012## Some value ##RES00034## Another value"
Dim parts As String() = input.Split(New String() {"##RES"}, StringSplitOptions.RemoveEmptyEntries)

For i As Integer = 0 To parts.Length - 1
    If i > 0 OrElse input.StartsWith("##RES") = True Then
        parts(i) = "##RES" & parts(i)
    End If
Next
Fredrik Mörk