Results 1 to 7 of 7
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Fun with Split() (VB6)

    Toying around with the Split() function, I stumbled upon a way to extract words from sentences. This method does not require any explicit use (even mention) of an array at all. I imagine the method could be useful in many situations e.g. extracting an entry from a CSV or other delimited text file.
    <div style="width: 100%; background-color: #FFFFFF;"><code><font color=black>
    <font color=blue>Sub</font color=blue> SplitTest()
    <font color=blue>Dim</font color=blue> Sentence <font color=blue>As</font color=blue> <font color=blue>String</font color=blue>

    Sentence = "Alas, poor Yorick! I knew him, Horatio"
    Debug.Print Split(Sentence)(2) & " and " & Split(Sentence)(6)
    Debug.Print Split("Extract the third word in this sentence")(2)

    <font color=blue>End</font color=blue> <font color=blue>Sub</font color=blue>
    </font color=black></code></div hiblock>
    Output:
    Yorick! and Horatio
    third

    Alan

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Fun with Split() (VB6)

    Split returns a single dimension array, no matter how you use it. Here's a variation that calls Split only once:

    Sub SplitTest()
    Dim Sentence As String
    Dim Words As Variant
    Sentence = "Alas, poor Yorick! I knew him, Horatio"
    Words = Split(Sentence)
    Debug.Print Words(2) & " and " & Words(6)

    End Sub
    Charlotte

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Fun with Split() (VB6)

    Yep <img src=/S/yep.gif border=0 alt=yep width=15 height=15>, that's the "standard" documented usage of Split(). What I like about the method I found is that there is no need to Dim or name a variant (array) at all. Handy I'd think, for "one off" extractions of specific record fields etc. I don't think it's documented, but it is consistent with what you say i.e. Split returns a zero-based single dimension array.

    Alan

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Fun with Split() (VB6)

    Yes, but in

    Debug.Print Split(Sentence)(2) & " and " & Split(Sentence)(6)
    Debug.Print Split("Extract the third word in this sentence")(2)

    the VBA interpreter must execute the Split function three times. Split is calculation intensive and uses a lot of memory. If you declare a variant, Split only needs to be executed once.

  5. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Fun with Split() (VB6)

    Yes, I realize the CPU/ memory implications. I was just pointing out what seems to be an undocumented syntax, possibly suited to a for "one off" usage, not requiring the explicit declaration of a variant (array). As always, it is up to the programmer to utilise the function judiciously.

    Alan

  6. #6
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Fun with Split() (VB6)

    For what it's worth, I usually use split as an array as you have, but I'm usually only interested in one piece of a string.
    Don Ceraso first brought it to my attention in this puzzle thread started by Kevin.
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  7. #7
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Fun with Split() (VB6)

    Thanks Sammy. You illustrate my point admirably <img src=/S/wink.gif border=0 alt=wink width=15 height=15>. Looks like my "discovery" was only about 3 years late though. I seem to be improving. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Alan

Posting Permissions

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