【问题标题】:vb.net string split with a rulevb.net 用规则分割字符串
【发布时间】:2012-08-31 16:09:06
【问题描述】:

我有 2 个字符串,我想对它们使用 String.Split() 方法,如果包含“Love You”的字符串我想做的是忽略空格并将其作为一个元素保存在数组中但是如果它只包含“爱”就正常保存了

str1 = "I Love Her"
str2 = "I Love You Not"


'no problem with splitting the first string
Dim strsplit1() As String = str1.Split(New String() {" "}, StringSplitOptions.None)

对于第二个字符串..我怎样才能忽略空格分隔符并将“爱你”保存为一个元素?

注意这只是一个例子,我的想法告诉我检查 indexofLove+1 但是我怎样才能得到 Love 的索引?!

【问题讨论】:

    标签: vb.net string split


    【解决方案1】:

    你可以在这里使用简单的技巧。 在拆分字符串之前将“Love You”替换为“Love_You”,拆分完成后,扫描接收到的数组并将“Love_You”替换回“Love you”。

    【讨论】:

    • 您甚至可以改进此解决方案(如果您的数据中有真正的“Love_You”字符串)。将“Love You”替换为“Love” & ChrW(17) & “You”,因为 ChrW(17) 绝对不是文字符号。
    【解决方案2】:

    这对我有用

     Dim strsplit2() As String = str2.Split(New String() {" "}, StringSplitOptions.None)
        Dim loveindex = Array.IndexOf(strsplit2, "Love")
        If strsplit2(loveindex + 1) = "You" Then
            strsplit2(loveindex) = "Love You"
        End If
    

    我不关心其余的,因为我只想检查 strsplit2(loveindex) 是否存在于预定义的 ArrayList 中

    所以

      For Each x As String In strsplit2
            If myarraylist.Contains(x) Then
                MsgBox(x)
                Exit For
            End If
        Next
    

    很抱歉没有提及,但您的解决方案对关心阵列其余部分的任何人都有好处。

    我的问题是,有两组,一组称为“Holmegaard”,一组称为“Holmegaard Lamps”,它们包含在产品名称中,我必须解析它们才能向每个组显示正确的图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      相关资源
      最近更新 更多