| 1 |
//---------------------------------------------------- |
| 2 |
// name: StringTokenizer |
| 3 |
// desc: Break a string into tokens using a whitespace delimiter |
| 4 |
// Iterate through the tokens using next() + more() |
| 5 |
// or get(i) to get the i-th token |
| 6 |
// |
| 7 |
// author: terry feng |
| 8 |
//---------------------------------------------------- |
| 9 |
|
| 10 |
StringTokenizer strtok; |
| 11 |
|
| 12 |
// set the string |
| 13 |
strtok.set( "Tokenize me, please!" ); |
| 14 |
|
| 15 |
// check how many tokens there are |
| 16 |
<<< "tokens found:", strtok.size() >>>; |
| 17 |
|
| 18 |
// iterate through the tokens |
| 19 |
while( strtok.more() ) |
| 20 |
{ |
| 21 |
// print current token |
| 22 |
<<< strtok.next(), "" >>>; |
| 23 |
} |
| 24 |
|
| 25 |
// reset the tokenizer |
| 26 |
strtok.reset(); |
| 27 |
|
| 28 |
string foo; |
| 29 |
|
| 30 |
// get the first token and pass it to foo |
| 31 |
strtok.next( foo ); |
| 32 |
<<< "first token:", foo >>>; |
| 33 |
|
| 34 |
// another way to get the first token |
| 35 |
<<< "first token:", strtok.get( 0 ) >>>; |
| 36 |
|
| 37 |
// get last token and pass it to foo |
| 38 |
strtok.get( strtok.size() - 1, foo ); |
| 39 |
<<< "last token:", foo >>>; |
| 40 |
|
| 41 |
// update delimiters |
| 42 |
strtok.delims( " ,!" ); |
| 43 |
<<< "using , and ! as delimters...", "" >>>; |
| 44 |
// iterate through the tokens |
| 45 |
while( strtok.more() ) |
| 46 |
{ |
| 47 |
// print current token |
| 48 |
<<< strtok.next(), "" >>>; |
| 49 |
} |