#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Location
    England
    Posts
    98
    Rep Power
    8

    Split a String? VB6


    Hey,

    I was wondering if there is a way to split a string and put it into two parts. Basically I'm looking for the first ':' in the string, and I want to split them up into the two parts?

    Thank You
  2. #2
  3. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    585
    If you're just looking to split on the first substring, you'd use InStr() to find the position of of the first ":" and then use Mid() twice to get the substring before it and the substring after it. A couple tests with these functions (the character indexes for these start at 1) will have you understanding the rest, I bet.

    Comments on this post

    • zynder agrees : Nice and easy. I posted a sample below.
    Joel B Fant
    "An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."

    .NET Must-Haves
    Tools: Reflector
    References: Threading in .NET
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Posts
    2
    Rep Power
    0

    Split in VB6


    Use built-in Split function.

    e.g.

    Dim arr() As String
    arr = Split("a;b;c", ";")
  6. #4
  7. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    585
    *sigh*

    He'd have to concatenate all but the first element of the resulting array of strings in order to get the desired result that the op described. That's wasteful.
    Joel B Fant
    "An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."

    .NET Must-Haves
    Tools: Reflector
    References: Threading in .NET
  8. #5
  9. Inherits Programmer.Slacker
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Location
    Between my Id and your Ego
    Posts
    2,351
    Rep Power
    726
    ?

    Don't follow your line of thinking, Lyon. i would use split here, as well...
    Fisherman

    "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein
  10. #6
  11. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    585
    He says very plainly that he wants to split a string into two strings on the first ":". Using Split() would split on all occurences of ":".

    So what happens when he uses Split() and eventually comes across a string with more then one ":" and suddenly the results are not the ones that were expected?
    Joel B Fant
    "An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."

    .NET Must-Haves
    Tools: Reflector
    References: Threading in .NET
  12. #7
  13. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,440
    Rep Power
    4539
    Regardless of which technique is used, *sigh* is not any VB statement I've ever seen

    Comments on this post

    • LyonHaert agrees : That's why is isn't in code tags. :P
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  14. #8
  15. Type Cast Exception
    Devshed Supreme Being (6500+ posts)

    Join Date
    Apr 2004
    Location
    OAKLAND CA | Adam's Point (Fairyland)
    Posts
    14,954
    Rep Power
    8617
    I was gonna weigh in on the Split() side as well, but after thinking about it I'm gonna go with Instr() as well ...

    Code:
    Sub SplitIt(strText As String)
        Dim SplitPoint As Integer
        Dim strPart1 As String
        Dim strPart2 As String
        SplitPoint = InStr(strText, ":")
        strPart1 = Left(strText, SplitPoint - 1)
        strPart2 = Right(strText, Len(strText) - SplitPoint)
        MsgBox "Part1 = " & strPart1 & vbCrLf & "Part2 = " & strPart2
    End Sub
    
    Sub SomeTests()
        SplitIt "Hello:World"
        SplitIt "This:Is:A:Lot:Of:Colons:To:Have"
        SplitIt "A::B::C"
    End Sub
    medialint.com

    “Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
  16. #9
  17. Inherits Programmer.Slacker
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Location
    Between my Id and your Ego
    Posts
    2,351
    Rep Power
    726
    He says very plainly that he wants to split a string into two strings on the first ":".
    Ahh - I was reading it differently. I was reading it as though it were the only ":" in the string.
    Fisherman

    "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2003
    Posts
    508
    Rep Power
    18
    Originally Posted by medialint
    I was gonna weigh in on the Split() side as well, but after thinking about it I'm gonna go with Instr() as well ...
    Would it be really inefficient to split it just for the first part, then use Right to get the rest?

    Code:
    Function SplitIt(strText) As String
        Dim SplitPoint As Integer
        Dim strPart1 As String
        Dim strPart2 As String
        SplitPoint = InStr(strText, ":")
        strPart1 = Left(strText, SplitPoint - 1)
        strPart2 = Right(strText, Len(strText) - SplitPoint)
    
        SplitIt = "SplitIt" & vbCrLf & "Part1 = " & strPart1 & vbCrLf & "Part2 = " & strPart2 & vbCrLf
    End Function
    
    Function SplitIt2(strText) As String
        Dim aParts() As String
        Dim strPart1 As String
        Dim strPart2 As String
    
        aParts = Split(strText, ":")
    
        strPart1 = aParts(0)
        strPart2 = Right(strText, Len(strText) - Len(strPart1) - 1)
    
        SplitIt2 = "SplitIt2" & vbCrLf & "Part1 = " & strPart1 & vbCrLf & "Part2 = " & strPart2 & vbCrLf
    End Function
    
    Sub SomeTests()
        Dim strToSplit As String
    
        strToSplit = "Hello:World"
        MsgBox SplitIt(strToSplit) & SplitIt2(strToSplit)
    
        strToSplit = "This:Is:A:Lot:Of:Colons:To:Have"
        MsgBox SplitIt(strToSplit) & SplitIt2(strToSplit)
    
        strToSplit = "A::B::C"
        MsgBox SplitIt(strToSplit) & SplitIt2(strToSplit)
    End Sub
  20. #11
  21. Not much of a contributor
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2006
    Location
    Hidden
    Posts
    1,009
    Rep Power
    1092
    Here is a simple version.

    Code:
    Private Sub Command1_Click()
    Dim myStr As String
    Dim xPos As Integer
    
    myStr = "This:is:a:test:string"
    xPos = InStr(1, myStr, ":")
    
    Debug.Print Mid$(myStr, 1, xPos - 1)
    Debug.Print Mid$(myStr, xPos + 1, Len(myStr))
    
    End Sub
    Hope it helps!
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    1
    Rep Power
    0
    Originally Posted by zynder
    Here is a simple version.
    Hope it helps!
    Lifted pretty much from the mirage source.


    Code:
    Sub TestSub()
    Dim Data, Splitter as string
    Dim DataPart() as string
    
    Splitter = ":"
    Data = "This is a test:And likely works too:Bet it did =P"
    Datapart = Split(Data, Splitter)
    
    MsgBox Datapart(0)
    MsgBox Datapart(1)
    MsgBox Datapart(2)
    
    End sub
  24. #13
  25. Type Cast Exception
    Devshed Supreme Being (6500+ posts)

    Join Date
    Apr 2004
    Location
    OAKLAND CA | Adam's Point (Fairyland)
    Posts
    14,954
    Rep Power
    8617
    Thanks for bouncing this year+ old thread but you missed the point.
    medialint.com

    “Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0
    Originally Posted by markvii
    Use built-in Split function.

    e.g.

    Dim arr() As String
    arr = Split("a;b;c", ";")
    You CAN use Split here, if you limit the number of outputs to 2

    Dim arr() As String
    arr = Split("Word:this_is_the_2:nd.part", ":",2)

    print arr(0) -> "Word"
    print arr(1) -> "this_is_the_2:nd.part"

    Otherwise, the instr() / mid$() combo is probably more efficient.

IMN logo majestic logo threadwatch logo seochat tools logo