2015-10-19 177 views
-4

我想將字符串拆分成部分,但無法弄清楚!將字符串拆分成部分

我主要是從字符串

"hello bye see you" 

讀取從"bye" to "you"

我試圖

Dim qnew() As String = tnew.Split(" ") 

但我被困在代碼的其他部分,我真的想一些幫幫我。 很抱歉,如果我不是最好的解釋的事情,至少我盡力了:/

+1

「你好再見見你」,從 「再見」,以 「你」 讀..沒得到那?你能詳細說明一下嗎? – DarkKnight

+1

請提供您的其他代碼,並嘗試解釋您的問題/要求。 –

+0

'String.Split()'不適用於From-To方式。設置一個斷點並將鼠標放在'qnew'上查看結果。仍然不明白嗎?哦,至少我已經盡力了* – Plutonix

回答

1

我假設你的預期輸出是。如果我理解正確,則下列方法可用於獲取所需的輸出:

在該串分成數組(splits())與分隔符" "和然後該陣列使用for loop得到byeyou之間的陣列中的字符串找出的byej)和youk)指數。

Function GETSTRINGBETWEEN(ByVal start As String, ByVal parent As String, ByVal [end] As String) 
     Dim output As String = "" 
     Dim splits() As String = parent.Split(" ") 
     Dim i As Integer 
     Dim j As Integer = Array.IndexOf(splits, start) 
     Dim k As Integer = Array.IndexOf(splits, [end]) 
     For i = j To k 
      If output = String.Empty Then 
       output = splits(i) 
      Else 
       output = output & " " & splits(i) 
      End If 
     Next 
     Return output 
    End Function 

用法:

Dim val As String 
val = GETSTRINGBETWEEN("bye", "hello bye see you", "you") 
'val="bye see you" 

Function GET_STRING_BETWEEN(ByVal start As String, ByVal parent As String, ByVal [end] As String) 
     Dim output As String 
     output = parent.Substring(parent.IndexOf(start) _ 
               , (parent.IndexOf([end]) _ 
                - parent.IndexOf(start)) _ 
                ).Replace(start, "").Replace([end], "") 
     output = start & output & [end] 
     Return output 
    End Function 

用法:

Dim val As String 
val = GET_STRING_BETWEEN("bye", "hello bye see you", "you") 
'val="bye see you" 
+0

謝謝!對不起,遲到的迴應,在我讀了這個之後,我想到了:) – Hawkeye111