2011-10-22 63 views
0

我有含有像這樣的字符串:vb.net:分割字符串

你好,這是一{測試,例如}

你好,這是一{測試,例如}一些其他文本

你好,這是一{測試,例如}某些其他文本{TEST1,例1}等等等等等等

現在是這樣的,我想什麼:

創建一個列表與所有的普通文本和所有的{}

所以例如之間的文本:

  • 您好,這是一個
  • 測試,例如

  • 您好,這是一個
  • 測試,例如
  • 一些其他文本

等(就像串看起來像第一個例子。

我該如何做到最好?

+0

你想要在兩個單獨的列表中的'}'之間的所有正常文本和所有文本? – ipr101

+0

不,他們必須在同一個名單上。 – Ruben

回答

0

試試這個,它使用正則表達式,而不是Split功能 -

Dim value As String = "hello this is an {test, example} some other text { test1, example1 } etc etc etc" 
Dim words As List(Of String) = new List(Of String) 

' Get a collection of matches. 
Dim matches As MatchCollection = Regex.Matches(value, "[^\{\}]+") 

For Each match As Match In matches 
    words.Add(match.Captures(0).Value.Trim) 
Next 

words名單現在應該包含所需的列表。

+0

非常感謝:D – Ruben