I'm working in a project that uses mostly VB.NET.
One day when debugging I found myself in class called Utility
In this class there are a number of slightly different methods like the one below.
Not the most efficient code I've seen.
With split you could do the same thing in one line.
Looking at the code it looks like it's ported from VB6, but that's no excuse, VB6 has split.
'*****************************************************************
' Name xGetItem
' Purpose Returns an item from a string
' Parameters
' sSource The string to get the item from
' sDelm the item delimiter
' n The (1-based) index for the item to get
'
' Note: If sDelm is blank, "," will be used, The order of the
' parameters is different than in most other string functions
'
' Example xGetItem("a,z,x,d,f",3) equals "x"
' Returns The nth item as a string
'*****************************************************************
Public Function xGetItem(ByVal sSource As String, ByVal n As Integer, Optional ByVal sDelm As String = ",") As String
On Error GoTo ErrorHandler
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim nCount As Integer
Dim nLspos As Integer
Dim nLength As Integer
Dim sDest As String
xGetItem = ""
sDest = ""
nCount = 0
nLspos = 1
If (sDelm = " ") Then
xGetItem = xGetToken(sSource, n)
Exit Function
End If
nLength = Len(sSource)
For i = 1 To nLength
If (Mid(sSource, i, 1) = sDelm) Then
nCount = nCount + 1
If (n = nCount) Then
k = 1
j = nLspos
While ((j <= nLength) And (Mid(sSource, j, 1) <> sDelm))
sDest = sDest & Mid(sSource, j, 1)
j = j + 1
k = k + 1
End While
xGetItem = sDest
Exit Function
Else
nLspos = i + 1
End If
End If
Next
nCount = nCount + 1
If (n = nCount) Then
k = 1
j = nLspos
While ((j <= nLength) And (Mid(sSource, j, 1) <> sDelm))
sDest = sDest & Mid(sSource, j, 1)
j = j + 1
k = k + 1
End While
End If
xGetItem = sDest
Exit Function
ErrorHandler:
RaiseError(Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext)
End Function 'xGetItem