Tokenizing a string

This forum is currently in read-only mode.
  • 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.

  • [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"

    You could use that, but I think you would have to force a delimiter when the person is typing in the editbox.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

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

    Mid(string, index, N)
        Returns the N characters after index in string. [/code: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.

    >

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

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

    Perfect Pixel

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

    Thanks.

  • 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:

    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.
  • 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:

    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.
    

    Thanks for the info Silent Cacophony I'll keep that in mind.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)