Debian Codegraph @

examples/string/replace.ck

raw
1 // replace all occurances of a substring with a different string
2 // (requires chuck-1.5.1.3 or higher) @nshaheed
3
4 // a string
5 "Hello, the world! Hello, the world!" => string str;
6
7 // replace instances of "the world" with "ChucK"
8 str.replace( "the world", "ChucK" );
9 // et voila
10 <<< str >>>;
11
12 // keep on replacing
13 str.replace( ", ChucK! Hello, ChucK!", " the world" );
14 str.replace( "H", "h" );
15 // print
16 <<< str >>>;
17
18 // replace starting at position 6
19 str.replace( 6, "cat" );
20 // et voila
21 <<< str >>>;
22
23 // string.replace( position, length, str )
24 for( int x : Std.range(0,10) )
25 {
26 // reset
27 str => string str2;
28 // replace
29 str2.replace( 6, x, "kitty" );
30 // print
31 <<< "replace( 6,", x, "):", str2 >>>;
32 }
33
34 // for string API documentation, see:
35 // https://chuck.stanford.edu/doc/reference/base.html#string