2013-02-23 65 views
1

我想在除了普通字母以外的所有內容的VB.NET中拆分字符串。VB.NET:除正常字母外的所有內容都拆分字符串

我試着寫使用Char.IsLetter(...)的功能,但它並沒有因爲某種原因工作也很好(我把評論的地方墜毀):

Private Function splitAtNonLetter(ByVal SplitString As String) As String() 
    Dim NonCharSplitArray As String() = {} 
    Dim ProcessValueTemp As String = String.Empty 
    For Each Letter As Char In SplitString 
     If Char.IsLetter(Letter) Then 
      ProcessValueTemp += Letter.ToString 
     Else 
      NonCharSplitArray(NonCharSplitArray.Length) = ProcessValueTemp 
      ProcessValueTemp = String.Empty 
     End If 
    Next 

    If ProcessValueTemp.Length > 0 Then 
     ' Crashes in the next line: Index out of range exception... 
     NonCharSplitArray(NonCharSplitArray.Length) = ProcessValueTemp 
    End If 

    Return NonCharSplitArray 
End Function 

(我試過使用正則表達式,但我已經從未使用它們之前,所以我並沒有真正管理得到它的任何工作)

有沒有辦法用正則表達式的做,或做你必須寫一個新功能它會如何工作?

回答

2

Regex.Split可能是要走的路,使用否定字符組。

例如:

Dim bits = Regex.Split(text, "[^a-zA-z]") 

或者以應對非ASCII字符,以及:

Dim bits = Regex.Split(text, "[^\p{L}]") 
+0

我不能讓你的最後解決方案的工作,它給了我(text.Length + 1)空的列表條目。 感謝您的回答,無論如何,您的第一個解決方案工作得很好:) – user2102293 2013-02-23 14:30:02

+0

@ user2102293:現在修復 - 抱歉給您帶來不便。 – 2013-02-23 14:33:05

+0

不錯,謝謝! 謝謝你SOO多爲你的答案:) 編輯:如果我有足夠的聲譽,我會upvote你的答案... – user2102293 2013-02-23 14:35:39

相關問題