TLex string parsing

Login to reply to this topic.
Thu, 2005-09-15 09:47
Joined: 2005-09-15
Forum posts: 5
Hey All,
    I'm trying to parse a space delimited string with several tokens in it and have had no luck. Every time I do a NextToken() I simply get the previous string, but with the front chopped off. Is this correct? Here's a bit of code to illustrate:

_LIT(KStringToParse, "One Two Three Four Five");

TLex lex(KStringToParse);

TPtrC one = lex.NextToken(); // Expecting: one = "One" Actually: one = "One Two Three Four Five";
TPtrC two = lex.NextToken(); // Expecting: two = "Two" Actually: two = "Two Three Four Five";
TPtrC three = lex.NextToken(); // Expecting: three = "Three" Actually: three = "Three Four Five";
TPtrC four = lex.NextToken(); // Expecting: four = "Four" Actually: four = "Four Five";
TPtrC five = lex.NextToken(); // Expecting: five = "Five" Actually: five = "Five";

I just wanna grab one token at a time and use them as I would a TDesC. Any ideas?

Thanks,
    scarab

Fri, 2005-09-16 07:33
Joined: 2005-09-15
Forum posts: 5
Re: TLex string parsing
Can anybody help out with this? A brief example modifying my original code above would help tremendously.

Thanks,
   Scarab
Fri, 2005-09-16 13:00
Joined: 2003-12-05
Forum posts: 672
Re: TLex string parsing
_LIT(KSpace, " ");
TPtrC one = lex.NextToken();
// one = "One Two Three Four Five";
TInt nextSpacePos = one.Locate(KSpace);
// nextSpacePos =3
TPtrC first = one.Left(nextSpacePos);
// first = "One"

TPtrC two = lex.NextToken();
// two = "Two Three Four Five
nextSpacePos = two.Locate(KSpace);
TPtrC second =two.Left(nextSpacePos);

etc. Would this do?
Sat, 2005-09-17 02:37
Joined: 2005-09-15
Forum posts: 5
Re: TLex string parsing
Hi Andreas,
    Something like that should work. I get compilation errors, however, on

     TInt nextSpacePos = one.Locate(KSpace);

and when I switch it up to to

     TInt nextSpacePos = one.Locate(' ');

it compiles fine, but nextSpacePos = -1 after that line executes. If I switch it to
   
     TInt nextSpacePos = one.Locate('e'); // nextSpacePos = 2 for _LIT(KStringToParse, "One Two Three Four Five");

I get a valid value. How come it won't find spaces? Do I need to escape the space character with something? I also tried

     TInt nextSpacePos = one.Locate(TChar(32)); //ASCII code for space

to no avail as well. What's going on?!

- scarab
Fri, 2007-12-07 17:32
Joined: 2006-01-21
Forum posts: 9
Re: TLex string parsing

change it to TInt nextSpacePos = token.Find(_L(" "));


--------------------
Roy Weinberg,
Symbian Development Manager,
SBSH Mobile Software.

  • Login to reply to this topic.