Friend NotInheritable Class StringSplitExtensions Private Sub New() End Sub _ Public Shared Function Split(this As [String], isSplitSymbol As Predicate(Of Char)) As String() Return Split(this, isSplitSymbol, Integer.MaxValue, StringSplitOptions.None) End Function _ Public Shared Function Split(this As [String], isSplitSymbol As Predicate(Of Char), options As StringSplitOptions) As String() Return Split(this, isSplitSymbol, Integer.MaxValue, options) End Function _ Public Shared Function Split(this As [String], isSplitSymbol As Predicate(Of Char), count As Integer, options As StringSplitOptions) As String() If count < 0 Then Throw New ArgumentOutOfRangeException("count", "Count cannot be less than zero.") End If If (options < StringSplitOptions.None) OrElse (options > StringSplitOptions.RemoveEmptyEntries) Then Throw New ArgumentException([String].Format("Illegal enum value: {0}.", CInt(options))) End If Dim removeEmptyEntries As Boolean = options = StringSplitOptions.RemoveEmptyEntries If (count = 0) OrElse (removeEmptyEntries AndAlso (this.Length = 0)) Then Return New String(-1) {} End If Dim sepList As Integer() = New Integer(this.Length - 1) {} Dim numReplaces As Integer = this.MakeSeparatorList(isSplitSymbol, sepList) If (numReplaces = 0) OrElse (count = 1) Then Return New String() {this} End If If removeEmptyEntries Then Return this.InternalSplitOmitEmptyEntries(sepList, numReplaces, count) Else Return this.InternalSplitKeepEmptyEntries(sepList, numReplaces, count) End If End Function _ Private Shared Function MakeSeparatorList(this As [String], isSplitSymbol As Predicate(Of Char), ByRef sepList As Integer()) As Integer Dim numReplaces As Integer = 0 Dim length As Integer = sepList.Length, j As Integer = 0 While (j < this.Length) AndAlso (numReplaces < length) If isSplitSymbol(this(j)) Then sepList(System.Math.Max(System.Threading.Interlocked.Increment(numReplaces),numReplaces - 1)) = j End If j += 1 End While Return numReplaces End Function _ Private Shared Function InternalSplitKeepEmptyEntries(this As [String], sepList As Integer(), numReplaces As Integer, count As Integer) As String() Dim startIndex As Integer = 0 Dim index As Integer = 0 count -= 1 Dim actualCount As Integer = If((numReplaces < count), numReplaces, count) Dim strArray As String() = New String(actualCount) {} Dim i As Integer = 0 While (i < actualCount) AndAlso (startIndex < this.Length) strArray(System.Math.Max(System.Threading.Interlocked.Increment(index),index - 1)) = this.Substring(startIndex, sepList(i) - startIndex) startIndex = sepList(i) + 1 i += 1 End While If (startIndex < this.Length) AndAlso (actualCount >= 0) Then strArray(index) = this.Substring(startIndex) Return strArray End If If index = actualCount Then strArray(index) = [String].Empty End If Return strArray End Function _ Private Shared Function InternalSplitOmitEmptyEntries(this As [String], sepList As Integer(), numReplaces As Integer, count As Integer) As String() Dim actualCount As Integer = If((numReplaces < count), (numReplaces + 1), count) Dim strArray As String() = New String(actualCount - 1) {} Dim startIndex As Integer = 0 Dim replaceIndex As Integer = 0 Dim i As Integer = 0 While (i < numReplaces) AndAlso (startIndex < this.Length) If (sepList(i) - startIndex) > 0 Then strArray(System.Math.Max(System.Threading.Interlocked.Increment(replaceIndex),replaceIndex - 1)) = this.Substring(startIndex, sepList(i) - startIndex) End If startIndex = sepList(i) + 1 If replaceIndex = (count - 1) Then While (i < (numReplaces - 1)) AndAlso (startIndex = sepList(System.Threading.Interlocked.Increment(i))) startIndex += 1 End While Exit While End If i += 1 End While If startIndex < this.Length Then strArray(System.Math.Max(System.Threading.Interlocked.Increment(replaceIndex),replaceIndex - 1)) = this.Substring(startIndex) End If Dim strArray2 As String() = strArray If replaceIndex <> actualCount Then strArray2 = New String(replaceIndex - 1) {} For j As Integer = 0 To replaceIndex - 1 strArray2(j) = strArray(j) Next End If Return strArray2 End Function End Class