Search code examples
arraysvb.netfunctionsplitletter

VB.NET: Split string at everything except normal letters


I'm trying to split a string in VB.NET at everything except normal letters.

I tried to write a function using Char.IsLetter(...) but it didn't work too well for some reason (I put a comment where it crashed):

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

(I tried to use Regular Expressions but I've never used them before so I didn't really manage to get it to work either)

Is there a way to do it with RegExps or do you have to write a new function and how would it work?


Solution

  • Regex.Split is probably the way to go, using a negative character group.

    For example:

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

    Or to cope with non-ASCII letters as well:

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