how would i do a split function in vb5?
Printable View
how would i do a split function in vb5?
I wrote this ages ago, I think it works fine:
VB Code:
Function SplitText(ByVal Text As String, _ Optional ByVal Delimeter As String = ";", _ Optional ByVal Limit As Long = -1, _ Optional ByVal Compare As VbCompareMethod = vbTextCompare) As String() On Local Error Resume Next 'Add the delimiter to the end of the text so that we return all strings Text = Text + Delimeter 'The Ubound of the array Dim Count As Integer Count = 0 'Find the start/end of the first string Dim StartPos As Long, EndPos As Single StartPos = 1 EndPos = InStr(1, Text, Delimeter, Compare) 'What gets returned Dim ReturnValue() As String 'Loop for all strings Do While EndPos > 0 And (Limit <= -1 Or EndPos <= Limit) 'Add the current string ReDim Preserve ReturnValue(Count) ReturnValue(Count) = Mid(Text, StartPos, EndPos - StartPos) 'Increment the length of the array Count = Count + 1 'Find the next string StartPos = EndPos + Len(Delimeter) EndPos = InStr(StartPos, Text, Delimeter, Compare) Loop 'Return the array SplitText = ReturnValue End Function
Try testing it with something like this:
VB Code:
Private Sub Command1_Click() Dim Texts() As String Texts = SplitText("String1;String2;String3;String4") Dim LoopCounter As Integer For LoopCounter = LBound(Texts) To UBound(Texts) MsgBox Texts(LoopCounter) Next LoopCounter End Sub
ty
ill see if it helps :)
all other posts are welcome too :)
VB Code:
Public Function Split(ByVal sIn As String, Optional sDelim As _ String, Optional nLimit As Long = -1, Optional bCompare As _ VbCompareMethod = vbBinaryCompare) As Variant Dim sRead As String, sOut() As String, nC As Integer If sDelim = "" Then Split = sIn End If sRead = ReadUntil(sIn, sDelim, bCompare) Do ReDim Preserve sOut(nC) sOut(nC) = sRead nC = nC + 1 If nLimit <> -1 And nC >= nLimit Then Exit Do sRead = ReadUntil(sIn, sDelim) Loop While sRead <> "" ReDim Preserve sOut(nC) sOut(nC) = sIn Split = sOut End Function