Tokenizing a string

For questions about using Classic.

Post » Sun Mar 27, 2011 2:35 pm

I want to tokenize a string (just one word and restricted to 9 letters) that I am getting from an editbox but I'm having a few problems.

Basically I want a user to enter their name (upto 9 letters) and then tokenize it so MINOR would be tokenized as M,I,N,O,R then I can play around with the letters/numbers.

I've tried using left(global('name'), 1) which works fine but I can't seem to get the 2nd letter in the word using left(global('name'),2 returns the 1st & 2nd letters in the word.

Has anyone got an example of this? I know I'm missing something really simple here but thought I'd ask for some help.

:)
B
46
S
23
G
7
Posts: 459
Reputation: 6,763

Post » Sun Mar 27, 2011 3:26 pm

[quote:2thuwlzu]GetToken(string, N [, delimiter])
Returns the Nth token in string using delimiter. The default delimiter is comma (,) if you do not specify your own. Examples: GetToken("a,b,c", 2) gives "b", GetToken("one|two|three|four", 2, "|") gives "two" [/quote:2thuwlzu]

You could use that, but I think you would have to force a delimiter when the person is typing in the editbox.
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Sun Mar 27, 2011 3:44 pm

[quote="Minor":3fh2qrg1]I've tried using left(global('name'), 1) which works fine but I can't seem to get the 2nd letter in the word using left(global('name'),2 returns the 1st & 2nd letters in the word.[/quote:3fh2qrg1]

Yeah well, you need to use the MID expression here!

[code:3fh2qrg1]Mid(string, index, N)
Returns the N characters after index in string. [/code:3fh2qrg1]
B
21
S
6
G
10
Posts: 1,024
Reputation: 7,445

Post » Sun Mar 27, 2011 6:24 pm

[quote="PixelRebirth":3l22y63m][quote="Minor":3l22y63m]I've tried using left(global('name'), 1) which works fine but I can't seem to get the 2nd letter in the word using left(global('name'),2 returns the 1st & 2nd letters in the word.[/quote:3l22y63m]

Yeah well, you need to use the MID expression here!

[code:3l22y63m]Mid(string, index, N)
Returns the N characters after index in string. [/code:3l22y63m][/quote:3l22y63m]

Perfect Pixel :)

Didn't try MID. Automatically thought it would just return the centre character in the string.

Thanks.

:)
B
46
S
23
G
7
Posts: 459
Reputation: 6,763

Post » Sun Mar 27, 2011 6:53 pm

Not sure if this will help you, but it seems a good point to interject in this thread for completeness.

You could possibly skip the tokenizing thing completely, and just use the Mid() expression to isolate the individual characters. The len() expression is a natural companion to it, also. Assuming that Mid() is 1-based instead of 0-based (not sure if that's correct), this pseudocode would tokenize a string into delimited single characters:

[code:3i2hfww5]string = "Minor"
newstring = ""

For index = 1 to len(string) - 1
newstring = newstring + mid(string, index, 1) + ","
Next index

newstring = newstring + right(string, 1)[/code:3i2hfww5]

newstring would equal "M,i,n,o,r" after that. BTW, subtract one from both ends of the For line if Mid() is 0-based.

That said, if all of the tokens will always be a fixed number of characters as above, then I'd skip the tokenstring expressions and just use the mid() expression whenever you need one of the characters.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Sun Mar 27, 2011 8:42 pm

[quote="Silent Cacophony":1ao0py00]Not sure if this will help you, but it seems a good point to interject in this thread for completeness.

You could possibly skip the tokenizing thing completely, and just use the Mid() expression to isolate the individual characters. The len() expression is a natural companion to it, also. Assuming that Mid() is 1-based instead of 0-based (not sure if that's correct), this pseudocode would tokenize a string into delimited single characters:

[code:1ao0py00]string = "Minor"
newstring = ""

For index = 1 to len(string) - 1
newstring = newstring + mid(string, index, 1) + ","
Next index

newstring = newstring + right(string, 1)[/code:1ao0py00]

newstring would equal "M,i,n,o,r" after that. BTW, subtract one from both ends of the For line if Mid() is 0-based.

That said, if all of the tokens will always be a fixed number of characters as above, then I'd skip the tokenstring expressions and just use the mid() expression whenever you need one of the characters.[/quote:1ao0py00]

Thanks for the info Silent Cacophony I'll keep that in mind.
B
46
S
23
G
7
Posts: 459
Reputation: 6,763


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 5 guests