Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 54,513 times

Related Categories

Split Function

VB 6 included a useful Split function that allowed you to convert delimited text (ie portions of text broken up by a certain character) into an array. This code shows you how to make one if you do not have VB 6. Pass a string, and a delimiter, and it will return an array containing each item between the delimiter.

Private Function Split(Expression As String, Optional Delimiter As String) As Variant
    Dim i As Long
    Dim lNextPos As Long
    Dim sText As String
    Dim lCount As Long
    Dim varTemp() As String
    For i = 1 To Len(Expression)
        
        lNextPos = InStr(i + 1, Expression, Delimiter)
        If lNextPos = 0 Then
            lNextPos = Len(Expression) + 1
        End If
        sText = Mid$(Expression, i, lNextPos - i)
        ReDim Preserve varTemp(lCount)
        varTemp(lCount) = sText
        lCount = lCount + 1
        i = lNextPos + Len(Delimiter) - 1
        
    Next
    Split = varTemp
End Function

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [127] Split Function

    Posted by sync_or_swim on 04 May 2006

    What exactly are you trying to do?  what language are you using? 

    If you are using vb5 or earlier and you want to split that string up you just have to include that function in your ...

  • Re: [127] Split Function

    Posted by Usher on 04 May 2006

    hey guys,
    so how can I cut string like this one :

    When I used this function (running someone else vb6 code on a vb5 IDE), I had to change the INextPos = Len(Expression) line to the one below, sinc...

  • How to use the Split Function

    Posted by Sunitha on 26 May 2005

    'This function is used to get the document name by splitting the file path at the delimitor.

    Public Function SplitXpath(ByRef FilePath As String, ByRef Delimitor As String) As String

    Dim strXpat...

  • Posted by LouisRose on 12 Aug 2003

    [quote]VB 6 included a useful Split function that allowed you to convert delimited text (ie portions of text broken up by a certain character) into an array. This code shows you how to make one if you...

  • All ready available

    Posted by OliverB on 12 Aug 2003

    Isn't this method already available in VB? ... search the VB Help for SPLIT and JOIN Functions