views:

263

answers:

4

Hi,

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!

A: 

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.

Xeon06
This example is C#, I don't speak VB, but you shouldn't have any trouble converting it.
Xeon06
+2  A: 
Dim words As String() = myStr.Split(new String() { "##" }, 
                                        StringSplitOptions.None)
womp
No... he asked for it in VB, not C#.
womp
Then lose the semicolon. ;)
Todd Ropog
Ah ha... thanks. Shows what I'm working with right now :)
womp
A: 

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.

DavRob60
A: 
    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
prabhats.net