2012-08-31 64 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燈」,我不得不只解析他們展現給他們每個人