How to split a string with multi-character delimiter in vb - asp.net?
How should I split a string separated by a multi开发者_C百科-character delimiter in VB?
i.e. If my string is say - Elephant##Monkey, How do I split it with "##" ?
Thanks!
Dim words As String() = myStr.Split(new String() { "##" },
StringSplitOptions.None)
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.
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
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
精彩评论