File: replace.ck

package info (click to toggle)
chuck 1.5.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,056 kB
  • sloc: cpp: 123,473; ansic: 35,893; javascript: 2,111; yacc: 609; makefile: 457; python: 174; perl: 86
file content (35 lines) | stat: -rw-r--r-- 821 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// replace all occurances of a substring with a different string
// (requires chuck-1.5.1.3 or higher) @nshaheed

// a string
"Hello, the world! Hello, the world!" => string str;

// replace instances of "the world" with "ChucK"
str.replace( "the world", "ChucK" );
// et voila
<<< str >>>;

// keep on replacing
str.replace( ", ChucK! Hello, ChucK!", " the world" );
str.replace( "H", "h" );
// print
<<< str >>>;

// replace starting at position 6
str.replace( 6, "cat" );
// et voila
<<< str >>>;

// string.replace( position, length, str )
for( int x : Std.range(0,10) )
{
    // reset
    str => string str2;
    // replace
    str2.replace( 6, x, "kitty" );
    // print
    <<< "replace( 6,", x, "):", str2 >>>;
}

// for string API documentation, see:
// https://chuck.stanford.edu/doc/reference/base.html#string