user355562
user355562

Reputation: 3373

How to split a string with multi-character delimiter in vb - asp.net?

How should I split a string separated by a multi-character delimiter in VB?

i.e. If my string is say - Elephant##Monkey, How do I split it with "##" ?

Thanks!

Upvotes: 14

Views: 47291

Answers (4)

Alex Turpin
Alex Turpin

Reputation: 47776

Use Regex.Split.

string whole = "Elephant##Monkey";
string[] split = Regex.Split(whole, "##");
foreach (string part in split)
    Console.WriteLine(part);

Be careful however, because this isn't just a string, it's a complete Regular Expression. Some characters might need escaping, etc. I suggest you look them up.

UPDATE- Here is the corresponding VB.NET code:

Dim whole As String = "Elephant##Monkey"
Dim split As String() = Regex.Split(whole, "##")
For Each part As String In split
    Console.WriteLine(part)
Next

Upvotes: 4

prabhats.net
prabhats.net

Reputation: 444

    Dim s As String = "Elephant##Monkey"
    Dim parts As String() = s.Split(New Char() {"##"c})

            Dim part As String
    For Each part In parts
        Console.WriteLine(part)
    Next

Upvotes: 1

DavRob60
DavRob60

Reputation: 3557

here in VB.NET

Dim s As String = "Elephant##Monkey1##M2onkey"
Dim a As String() = Split(s, "##", , CompareMethod.Text)

ref : msdn check the Alice and Bob example.

Upvotes: 5

womp
womp

Reputation: 116977

Dim words As String() = myStr.Split(new String() { "##" }, 
                                        StringSplitOptions.None)

Upvotes: 25

Related Questions