Search code examples
vb.netsplitcurly-brackets

How to split a string containing {hello}{world} curley brackets but keep the brackets (vb.net)


I'm trying to find a way of splitting a string that contains 2 {}{} brackets but after it's split it keeps the brackets.

Before word = {XXXX}{XXXX}

After

  • Word(1) = {XXXX}

  • word(2) = {XXXX}

I tried using split but this always removes the }{ which I want to keep. Somebody get me out of my misery!! I'm using vb.net.

Dim word As String = "{hello}{world}" 
Dim wordArr As String() = word.Split("}")

Solution

  • This will work:

    Dim word As String = "{hello}{world}"
        Dim wordArr As String() = word.Split({"}"}, StringSplitOptions.RemoveEmptyEntries)
        Dim lst1 As New List(Of String)
        For Each l In wordArr
            lst1.Add(l & "}")
        Next
        wordArr = lst1.ToArray