History

Update token.ck

terry feng committed 3 years ago

Commit 91d181a1 parent 3c0df031 file · +33 −8Browse files

Showing the changes to examples/string (1 of 1 file). Show all

Unified · Split

examples/string/token.ck+33−8
@@ -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+//----------------------------------------------------
29
3−// make one
4−StringTokenizer tok;
10+StringTokenizer strtok;
511
612 // set the string
7−tok.set( "tokenize me please!" );
13+strtok.set( "Tokenize me please!" );
814
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() )
1120 {
12− // print
13− <<< tok.next(), "" >>>;
21+ // print current token
22+ <<< strtok.next(), "" >>>;
1423 }
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 >>>;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t