Get the n-th string faster (1 Viewer)

Status
Not open for further replies.

Guus2005

AWF VIP
Local time
Today, 11:09
Joined
Jun 26, 2007
Messages
2,645
This new function runs faster than this one
Return the n-th string using a separator.
Code:
Public Function GetPart2(strString As String, strSep As String, intPart As Integer) As String
'Guus2005 - 2006
'New GetPart function without recursion, returns part "intPart" of "strString" with "strSep" as a separator
'Uses an array and the Split() function.
    Dim arr() As String
    Dim intHigh As Integer

    On Error GoTo Err_GetPart2

    arr = Split(strString, strSep)
    'get number of items in the array
    intHigh = UBound(arr)
    
    If intPart <= 0 Then
        intPart = 1 'Get the first item
    ElseIf intPart >= intHigh Then
        intPart = intHigh + 1 'Get the last item
    End If
    
    GetPart2 = arr(intPart - 1)

Exit_GetPart2:
    Exit Function

Err_GetPart2:
    msgbox  Err & " " & Error$, "GetPart2",
    Resume Exit_GetPart2
End Function
Enjoy!
 
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom