使用规则拆分vb.net字符串



我有两个字符串,我想对它们使用String.Split()方法,如果字符串中包含"Love You",我想做的是忽略空间并将其作为一个元素保存在数组中。但是,如果仅包含"Love",则正常保存

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)

对于第二个字符串。。我怎么能忽略空格分隔符并将"爱你"保存为一个元素呢?

注意,这只是一个例子,我的大脑告诉我检查爱的指数+1,但我如何才能得到爱的指数?!

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

以下是

 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",一组叫做"Holmegard Lamps",这些都包含在产品名称中,我不得不对它们进行解析,只为它们中的每一个显示正确的图像

最新更新