Wiki

root/cobra/trunk/Tests/100-basics/062-string-substitution.cobra

Revision 2631, 1.3 KB (checked in by Charles.Esterbrook, 5 months ago)

JVM back-end progress.
credit:hopscc

  • Property svn:eol-style set to native
Line 
1namespace Test
2
3    class Test
4
5        def main
6            is shared
7
8            i as int = 3
9            j as int = 7
10
11            # substitution works with single quotes
12            assert ' [i] '==' 3 '
13            assert '[i] '=='3 '
14            assert ' [i]'==' 3'
15            assert '[i]'=='3'
16
17            assert '[i] [j]'=='3 7'
18            assert '[i][j]'=='37'
19
20            name as String = 'world'
21            assert 'Hello, [name].'=='Hello, world.'
22
23            # nested brackets
24            s as String = 'abc'
25            i = 1
26            #print 'the letter at [i] is [s[i]].'
27            #assert 'the letter at [i] is [s[i]].'=='the letter at 1 is b.'
28            si = s[i]
29            assert 'the letter at [i] is [si].'=='the letter at 1 is b.'
30
31            # substitution works with double quotes
32            i = 3
33            assert " [i] "==' 3 '
34            assert "[i] "=='3 '
35            assert " [i]"==' 3'
36            assert "[i]"=='3'
37            assert "[i] [j]"=='3 7'
38            assert "[i][j]"=='37'
39            assert "Hello, [name]."=='Hello, world.'
40
41            # escape [ with backslash: 'do not \[sub]'
42            foo = 1
43            bar = 2
44            assert 'foo \[bar]' == r'foo [bar]'
45            assert 'foo \[bar] [foo]' == r'foo [bar] 1'
46            assert 'foo [bar] \[foo]' == r'foo 2 [foo]'
47
48            # formatting - moved to 062-string-substitution-fmt
49            #mol as int = 42
50            #assert '[mol:N]'=='42.00'
51            #assert "[mol:N]"=='42.00'
52            #assert '[mol:%2d]'=='42.00' # jvm
53
54            # the ns prefix means "no sub" or "no substitution"
55            assert ns'aoeu'=='aoeu'
56            assert ns'aoeu'.length==4
57            assert ns'[i]'.length==3
Note: See TracBrowser for help on using the browser.