Debian Codegraph @

src/test/01-Basic/260-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 <<< "str.charAt( 0 ):", str.charAt( 0 ) >>>;
14 // read 10th char
15 <<< "str.charAt( 10 ):", str.charAt( 10 ) >>>;
16 // test character by index
17 str.setCharAt( 0, 'T' );
18 <<< "str.setCharAt( 0, 'T' ):", str >>>;
19
20 // test substring -- from the 10th char
21 <<< "str.substring( 10 ):", str.substring( 10 ) >>>;
22 // test substring -- from 10th char, for 10 chars
23 <<< "str.substring( 10, 10 ):", str.substring(10, 10) >>>;
24
25 // insert
26 str.insert( 36, "old " );
27 <<< "str.insert( 36, \"old \" ):", str >>>;
28
29 // test erase
30 str.erase( 40, 5 );
31 <<< "str.erase( 40, 5 ):", str >>>;
32
33 // test replace
34 str.replace( 40, "cat" );
35 <<< "str.replace( 40, \"cat\" ):", str >>>;
36 // test replace
37 str.replace( 4, 5, "slow" );
38 <<< "str.replace( 4, 5, \"slow\" ):", str >>>;
39
40 // test replace | 1.5.1.3 or higher
41 // str.replace( "slow brown", "hungry" );
42 // <<< "str.replace( \"slow brown\", \"hungry\" ):", str >>>;
43
44 // test find -- index of letter, return -1 if not found
45 <<< "str.find('x'):", str.find('x') >>>;
46 // test find
47 <<< "str.find('x', 15):", str.find('x', 15) >>>;
48 // test find
49 <<< "str.find(\"fox\"):", str.find("fox") >>>;
50 // test find
51 <<< "str.find(\"fox\", 20):", str.find("fox", 20) >>>;
52
53 // test rfind
54 <<< "str.rfind('o'):", str.rfind('o') >>>;
55 // test rfind
56 <<< "str.rfind('o', 20):", str.rfind('o', 20) >>>;
57 // test rfind
58 <<< "str.rfind(\"fox\"):", str.rfind("fox") >>>;
59 // test rfind
60 <<< "str.rfind(\"fox\", 20):", str.rfind("fox", 20) >>>;