Results 1 to 4 of 4

Thread: Split in vb5

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    115

    Split in vb5

    how would i do a split function in vb5?

  2. #2
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    I wrote this ages ago, I think it works fine:

    VB Code:
    1. Function SplitText(ByVal Text As String, _
    2.     Optional ByVal Delimeter As String = ";", _
    3.     Optional ByVal Limit As Long = -1, _
    4.     Optional ByVal Compare As VbCompareMethod = vbTextCompare) As String()
    5.     On Local Error Resume Next
    6.     'Add the delimiter to the end of the text so that we return all strings
    7.     Text = Text + Delimeter
    8.    
    9.     'The Ubound of the array
    10.     Dim Count As Integer
    11.     Count = 0
    12.    
    13.     'Find the start/end of the first string
    14.     Dim StartPos As Long, EndPos As Single
    15.     StartPos = 1
    16.     EndPos = InStr(1, Text, Delimeter, Compare)
    17.    
    18.     'What gets returned
    19.     Dim ReturnValue() As String
    20.    
    21.     'Loop for all strings
    22.     Do While EndPos > 0 And (Limit <= -1 Or EndPos <= Limit)
    23.         'Add the current string
    24.         ReDim Preserve ReturnValue(Count)
    25.         ReturnValue(Count) = Mid(Text, StartPos, EndPos - StartPos)
    26.         'Increment the length of the array
    27.         Count = Count + 1
    28.         'Find the next string
    29.         StartPos = EndPos + Len(Delimeter)
    30.         EndPos = InStr(StartPos, Text, Delimeter, Compare)
    31.     Loop
    32.     'Return the array
    33.     SplitText = ReturnValue
    34. End Function


    Try testing it with something like this:

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim Texts() As String
    3.     Texts = SplitText("String1;String2;String3;String4")
    4.     Dim LoopCounter As Integer
    5.     For LoopCounter = LBound(Texts) To UBound(Texts)
    6.         MsgBox Texts(LoopCounter)
    7.     Next LoopCounter
    8. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    115
    ty

    ill see if it helps






    all other posts are welcome too

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    VB Code:
    1. Public Function Split(ByVal sIn As String, Optional sDelim As _
    2.             String, Optional nLimit As Long = -1, Optional bCompare As _
    3.              VbCompareMethod = vbBinaryCompare) As Variant
    4.           Dim sRead As String, sOut() As String, nC As Integer
    5.           If sDelim = "" Then
    6.               Split = sIn
    7.           End If
    8.           sRead = ReadUntil(sIn, sDelim, bCompare)
    9.           Do
    10.               ReDim Preserve sOut(nC)
    11.               sOut(nC) = sRead
    12.               nC = nC + 1
    13.               If nLimit <> -1 And nC >= nLimit Then Exit Do
    14.               sRead = ReadUntil(sIn, sDelim)
    15.           Loop While sRead <> ""
    16.           ReDim Preserve sOut(nC)
    17.           sOut(nC) = sIn
    18.           Split = sOut
    19.       End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width