Showing the changes to examples/string (1 of 1 file). Show all
Unified · Split
| @@ -1,14 +1,39 @@ | ||
| 1 | −// the very hacked StringTokenizer | |
| 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 | +//---------------------------------------------------- | |
| 2 | 9 | |
| 3 | −// make one | |
| 4 | −StringTokenizer tok; | |
| 10 | +StringTokenizer strtok; | |
| 5 | 11 | |
| 6 | 12 | // set the string |
| 7 | −tok.set( "tokenize me please!" ); | |
| 13 | +strtok.set( "Tokenize me please!" ); | |
| 8 | 14 | |
| 9 | −// iterate | |
| 10 | −while( tok.more() ) | |
| 15 | +// check how many tokens there are | |
| 16 | +<<< "tokens found:", strtok.size() >>>; | |
| 17 | + | |
| 18 | +// iterate through the tokens | |
| 19 | +while( strtok.more() ) | |
| 11 | 20 | { |
| 12 | ||
| 13 | − <<< tok.next(), "" >>>; | |
| 21 | + // print current token | |
| 22 | + <<< strtok.next(), "" >>>; | |
| 14 | 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 >>>; | |