【问题标题】:How to split a string with multi-character delimiter in vb - asp.net?如何在 vb - asp.net 中使用多字符分隔符拆分字符串?
【发布时间】:2011-03-06 11:00:25
【问题描述】:

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

即如果我的字符串是 - Elephant##Monkey,我该如何用“##”分割它?

谢谢!

【问题讨论】:

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


    【解决方案1】:
    Dim words As String() = myStr.Split(new String() { "##" }, 
                                            StringSplitOptions.None)
    

    【讨论】:

      【解决方案2】:

      在 VB.NET 中

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

      ref : msdn 查看 Alice 和 Bob 示例。

      【讨论】:

      • “alice and bob”的答案是指 microsoft.visualbasic.split 方法,而不是 system.text.replace 方法。不过,这也不错! microsoft.visualbasic 包含许多有用的实用程序,甚至 c# 用户也可以使用。
      【解决方案3】:

      使用Regex.Split

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

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

      更新-这是相应的 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
      

      【讨论】:

      • 这个例子是C#,我不会说VB,但你应该不会有任何转换问题。
      【解决方案4】:
          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
      

      【讨论】:

        猜你喜欢
        • 2011-12-26
        • 2015-06-29
        • 1970-01-01
        • 1970-01-01
        • 2011-11-04
        • 2012-09-04
        • 1970-01-01
        • 2013-03-03
        相关资源
        最近更新 更多