2012-08-31 65 views
1

我有2个字符串,我想对他们使用String.Split()方法,如果字符串中包含“爱你”,我想要做的是忽略空间并将其保存在数组中作为一个元素然而,如果它包含“爱”仅保存正常vb.net字符串与规则拆分

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) 

对于第二string..how我可以忽略的空间分离器和“爱你”另存为一个元素?

请注意,这仅仅是一个例子,我的心灵告诉我检查indexofLove + 1,但我怎样才能得到爱的指数?!

回答

5

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

+2

右图像你甚至可以改进这个解决方案(如果你的数据中有真正的“Love_You”字符串)。将“Love You”替换为“Love”&ChrW(17)&“You”,因为ChrW(17)绝对不是文字符号。 – Dima

1

这里是我工作

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 

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

我的问题是,有这些都包含在产品名称2组,一个名为“Holmegaard”和一个叫“Holmegaard灯”,我不得不只解析他们展现给他们每个人