File: token.ck

package info (click to toggle)
chuck 1.5.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 41,260 kB
  • sloc: cpp: 124,539; ansic: 35,893; javascript: 2,111; yacc: 609; makefile: 457; python: 174; perl: 86
file content (49 lines) | stat: -rw-r--r-- 1,134 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//----------------------------------------------------
// name: StringTokenizer
// desc: Break a string into tokens using a whitespace delimiter
//       Iterate through the tokens using next() + more() 
//       or get(i) to get the i-th token
//
// author: terry feng
//----------------------------------------------------

StringTokenizer strtok;

// set the string
strtok.set( "Tokenize me, please!" );

// check how many tokens there are
<<< "tokens found:", strtok.size() >>>;

// iterate through the tokens
while( strtok.more() )
{
    // print current token
    <<< strtok.next(), "" >>>;
}

// reset the tokenizer
strtok.reset();

string foo;

// get the first token and pass it to foo
strtok.next( foo );
<<< "first token:", foo >>>;

// another way to get the first token
<<< "first token:", strtok.get( 0 ) >>>;

// get last token and pass it to foo
strtok.get( strtok.size() - 1, foo );
<<< "last token:", foo >>>;

// update delimiters
strtok.delims( " ,!" );
<<< "using , and ! as delimters...", "" >>>;
// iterate through the tokens
while( strtok.more() )
{
    // print current token
    <<< strtok.next(), "" >>>;
}