如何在vb - asp.net中拆分带有多字符分隔符的字符串?

时间:2010-06-21 18:12:44

标签: .net asp.net vb.net string

如何在VB中拆分由多字符分隔符分隔的字符串?

即。如果我的字符串是 - 大象##猴子,如何将其拆分为“##”?

谢谢!

4 个答案:

答案 0 :(得分:21)

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

答案 1 :(得分:5)

这里是VB.NET

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

ref:msdn检查Alice和Bob示例。

答案 2 :(得分:4)

使用Regex.Split

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

但要小心,因为这不仅仅是一个字符串,它是一个完整的正则表达式。有些角色可能需要转义,等等。我建议你查看它们。

UPDATE-这是相应的VB.NET代码:

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

答案 3 :(得分:1)

    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
相关问题