Debian Codegraph @

examples/string/strops2.ck

raw
1 // strops2.ck: more string operations
2 //
3 // for string API documentation, see:
4 // https://chuck.stanford.edu/doc/reference/base.html#string
5
6 // a string
7 "the quick brown fox jumped over the lazy dog" @=> string str;
8
9 // print string
10 <<< "str:", str >>>;
11
12 // read the first char (as int)
13 <<< "\nstr.charAt( 0 ):", str.charAt( 0 ) >>>;
14 // read 10th char
15 <<< "str.charAt( 10 ):", str.charAt( 10 ) >>>;
16 // set character by index
17 str.setCharAt( 0, 'T' );
18 <<< "str.setCharAt( 0, 'T' ):", str >>>;
19 //-----------------------------------------------
20 // same as above, but using strings as characters
21 //-----------------------------------------------
22 // read the first char (as string)
23 <<< "str.charAt2( 0 ):", str.charAt2( 0 ) >>>;
24 // read 10th char (as string)
25 <<< "str.charAt2( 10 ):", str.charAt2( 10 ) >>>;
26 // set character by index (using a string)
27 str.setCharAt2( 0, "t" );
28 <<< "str.setCharAt2( 0, \"t\" ):", str >>>;
29
30 // test substring -- from the 10th char
31 <<< "\nstr.substring( 10 ):", str.substring( 10 ) >>>;
32 // test substring -- from 10th char, for 10 chars
33 <<< "str.substring( 10, 10 ):", str.substring(10, 10) >>>;
34
35 // insert
36 str.insert( 36, "old " );
37 <<< "str.insert( 36, \"old \" ):", str >>>;
38
39 // test erase
40 str.erase( 40, 5 );
41 <<< "str.erase( 40, 5 ):", str >>>;
42
43 // test replace
44 str.replace( 40, "cat" );
45 <<< "str.replace( 40, \"cat\" ):", str >>>;
46 // test replace
47 str.replace( 4, 5, "slow" );
48 <<< "str.replace( 4, 5, \"slow\" ):", str >>>;
49
50 // test replace | 1.5.1.3 or higher
51 // str.replace( "slow brown", "hungry" );
52 // <<< "str.replace( \"slow brown\", \"hungry\" ):", str >>>;
53
54 // test find -- index of letter, return -1 if not found
55 <<< "\nstr.find('x'):", str.find('x') >>>;
56 // test find
57 <<< "str.find('x', 15):", str.find('x', 15) >>>;
58 // test find
59 <<< "str.find(\"fox\"):", str.find("fox") >>>;
60 // test find
61 <<< "str.find(\"fox\", 20):", str.find("fox", 20) >>>;
62
63 // test rfind
64 <<< "str.rfind('o'):", str.rfind('o') >>>;
65 // test rfind
66 <<< "str.rfind('o', 20):", str.rfind('o', 20) >>>;
67 // test rfind
68 <<< "str.rfind(\"fox\"):", str.rfind("fox") >>>;
69 // test rfind
70 <<< "str.rfind(\"fox\", 20):", str.rfind("fox", 20) >>>;