Results 1 to 8 of 8

Thread: end point of a string

  1. #1

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    end point of a string

    I just can't get this one, though i'm sure its an easy answer.

    If you have a string like this,

    string = 1,a|2,b|3,c|4,c

    I want to extract (1 and a) and (2 and b) etc.. which is fine, i can use the split function twice in a for loop, but i can't work out how to set the max limit of the loop, for example

    Dim firstsplit() as string

    firstsplit = split(string,"|")

    for i = 0 to max

    msgbox(firstsplit(i))

    next i

    How do i define 'max' ? . If it goes past the length of the string i get an, 'out of subscript error' .

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Use instead:

    For LBound(firstsplit) To Ubound(firstsplit)

    EDIT:

    Rather...

    For i = Lbound(firstsplit) To Ubound(firstsplit)
    '
    Next
    Last edited by leinad31; Apr 12th, 2004 at 05:58 AM.

  3. #3
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048
    if youre using split you may aswell start at 0

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Jmacp:

    The LBound() function returns with the lowest bound of an array. Since you are using split(), the lowest will always be 0.

    The UBound() function returns the highest bound of an array. Since you are using Split(), it will return the amount of tokens that were split (which is what you want).


    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    Also, you could do it something like this:

    Max = Len(strString) - Len(Replace(strString, "|"))) + 1 'The 1 is in there because you always have 1 fewer seperators than elements

    It's probably somewhat more efficient, though it would only be noticeable if it was called a few million times.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Damn, I never thought of it that way. When I wrote a small scripting language I looked for arguments by looping through a string with InStr() and adding 1 to the number of times "," occurred. Where were you when I did that!


    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    lol

    Actually, I don't believe I came up with it either. I do believe I read it here some time ago. Meh, it's probably Martin's; he makes everything
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by jemidiah
    Also, you could do it something like this:

    Max = Len(strString) - Len(Replace(strString, "|"))) + 1 'The 1 is in there because you always have 1 fewer seperators than elements

    It's probably somewhat more efficient, though it would only be noticeable if it was called a few million times.
    Not bad, excepting that he's already splitting the text....

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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