Is there a better approach than this for loop?
Basically, for each character in a charArray created from the string to split, if the character is a letter, digit, or hyphen, I append it to a temporary string. When I see a split character or the last character, and the temporary string has value, I add it to the collection to return.
For i As Integer = 0 To (charArrayLength - 1)
charToInspect = CChar(charArray.GetValue(i))
If IsLetterOrDigit(charToInspect) Or charToInspect = hyphen Then
tempString = tempString + charToInspect
If i = (charArrayLength - 1) Then
listOfStringToReturn.Add(tempString)
End If
ElseIf tempString.Length > 0 Then
listOfStringToReturn.Add(tempString)
tempString = String.Empty
End If
Next
Return listOfStringToReturn
This logic is already provided by the
string.Splitmethod, which takes a list of possible separator characters to split on.If you have more complex delimiters, you can look at
Regex.Split.