|
-
Apr 12th, 2004, 05:45 AM
#1
Thread Starter
Frenzied Member
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' .
-
Apr 12th, 2004, 05:49 AM
#2
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.
-
Apr 12th, 2004, 06:01 AM
#3
Frenzied Member
if youre using split you may aswell start at 0
-
Apr 12th, 2004, 06:33 AM
#4
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
-
Apr 12th, 2004, 02:31 PM
#5
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.
-
Apr 12th, 2004, 02:34 PM
#6
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
-
Apr 12th, 2004, 02:37 PM
#7
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.
-
Apr 12th, 2004, 03:12 PM
#8
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|