|
| 1 | +void println (string str) { |
| 2 | + stdout.printf ("%s\n", str); |
| 3 | +} |
| 4 | + |
| 5 | +void main () { |
| 6 | + |
| 7 | + /* Strings are of data type 'string' and can be concatenated with the plus |
| 8 | + * operator resulting in a new string: |
| 9 | + */ |
| 10 | + string a = "Concatenated "; |
| 11 | + string b = "string"; |
| 12 | + string c = a + b; |
| 13 | + println (c); |
| 14 | + |
| 15 | + /* If you want to have a mutable string you should use StringBuilder. |
| 16 | + * With its help you are able to build strings ad libitum by prepending, |
| 17 | + * appending, inserting or removing parts. It's faster than multiple |
| 18 | + * concatenations. In order to obtain the final product you access the |
| 19 | + * field '.str'. |
| 20 | + */ |
| 21 | + var builder = new StringBuilder (); |
| 22 | + builder.append ("built "); |
| 23 | + builder.prepend ("String "); |
| 24 | + builder.append ("StringBuilder"); |
| 25 | + builder.append_unichar ('.'); |
| 26 | + builder.insert (13, "by "); |
| 27 | + println (builder.str); // => "String built by StringBuilder." |
| 28 | + |
| 29 | + /* You can create a new string according to a format string by calling the |
| 30 | + * method 'printf' on it. Format strings follow the usual rules, known from |
| 31 | + * C and similar programming languages. |
| 32 | + */ |
| 33 | + string formatted = "PI %s equals %g.".printf ("approximately", Math.PI); |
| 34 | + println (formatted); |
| 35 | + |
| 36 | + /* Strings prefixed with '@' are string templates. They can evaluate |
| 37 | + * embedded variables and expressions prefixed with '$'. |
| 38 | + * Since Vala 0.7.8. |
| 39 | + */ |
| 40 | + string name = "Dave"; |
| 41 | + println (@"Good morning, $name!"); |
| 42 | + println (@"4 + 3 = $(4 + 3)"); |
| 43 | + |
| 44 | + /* The equality operator compares the content of two strings, contrary to |
| 45 | + * Java's behaviour which in this case would check for referential equality. |
| 46 | + */ |
| 47 | + a = "foo"; |
| 48 | + b = "foo"; |
| 49 | + if (a == b) { |
| 50 | + println ("String == operator compares content, not reference."); |
| 51 | + } else { |
| 52 | + assert_not_reached (); |
| 53 | + } |
| 54 | + |
| 55 | + /* You can compare strings in lexicographical order with the < and > operators: */ |
| 56 | + if ("blue" < "red" && "orange" > "green") { |
| 57 | + println ("blue is less than red and orange is greater than green"); |
| 58 | + } |
| 59 | + |
| 60 | + // Switch statement |
| 61 | + string pl = "vala"; |
| 62 | + switch (pl) { |
| 63 | + case "java": |
| 64 | + assert_not_reached (); |
| 65 | + case "vala": |
| 66 | + println ("Switch statement works fine with strings."); |
| 67 | + break; |
| 68 | + case "ruby": |
| 69 | + assert_not_reached (); |
| 70 | + } |
| 71 | + |
| 72 | + /* You can apply various operations on strings. Here's a small selection: */ |
| 73 | + println ("from lower case to upper case".up ()); |
| 74 | + println ("reversed string".reverse ()); |
| 75 | + println ("...substring...".substring (3, 9)); |
| 76 | + |
| 77 | + /* The 'in' keyword is syntactic sugar for checking if one string contains |
| 78 | + * another string. The following expression is identical to |
| 79 | + * "swordfish".contains ("word") |
| 80 | + */ |
| 81 | + if ("word" in "swordfish") { |
| 82 | + println ("word is a part of swordfish"); |
| 83 | + } |
| 84 | + |
| 85 | + // Regular expressions |
| 86 | + try { |
| 87 | + var regex = new Regex ("(jaguar|tiger|leopard)"); |
| 88 | + string animals = "wolf, tiger, eagle, jaguar, leopard, bear"; |
| 89 | + println (regex.replace (animals, -1, 0, "kitty")); |
| 90 | + } catch (RegexError e) { |
| 91 | + warning ("%s", e.message); |
| 92 | + } |
| 93 | +} |
0 commit comments