update repository
[cmdllinux.git] / bash_n_examples / vala / vala.vala
bloba13a888445b3131d7da3d6a78654f2895a4eea35
1 void println (string str) {
2 stdout.printf ("%s\n", str);
4 void main () {
5 /* Strings are of data type 'string' and can be concatenated with the plus
6 * operator resulting in a new string:
7 */
8 string a = "Concatenated ";
9 string b = "string";
10 string c = a + b;
11 println (c);
12 /* If you want to have a mutable string you should use StringBuilder.
13 * With its help you are able to build strings ad libitum by prepending,
14 * appending, inserting or removing parts. It's faster than multiple
15 * concatenations. In order to obtain the final product you access the
16 * field '.str'.
18 var builder = new StringBuilder ();
19 builder.append ("built ");
20 builder.prepend ("String ");
21 builder.append ("StringBuilder");
22 builder.append_unichar ('.');
23 builder.insert (13, "by ");
24 println (builder.str); // => "String built by StringBuilder."
25 /* You can create a new string according to a format string by calling the
26 * method 'printf' on it. Format strings follow the usual rules, known from
27 * C and similar programming languages.
29 string formatted = "PI %s equals %g.".printf ("approximately", Math.PI);
30 println (formatted);
31 /* Strings prefixed with '@' are string templates. They can evaluate
32 * embedded variables and expressions prefixed with '$'.
33 * Since Vala 0.7.8.
35 string name = "Dave";
36 println (@"Good morning, $name!");
37 println (@"4 + 3 = $(4 + 3)");
38 /* The equality operator compares the content of two strings, contrary to
39 * Java's behaviour which in this case would check for referential equality.
41 a = "foo";
42 b = "foo";
43 if (a == b) {
44 println ("String == operator compares content, not reference.");
45 } else {
46 assert_not_reached ();
48 /* You can compare strings lexicographically with the < and > operators: */
49 if ("blue" < "red" && "orange" > "green") {
50 // That's correct
52 // Switch statement
53 string pl = "vala";
54 switch (pl) {
55 case "java":
56 assert_not_reached ();
57 case "vala":
58 println ("Switch statement works fine with strings.");
59 break;
60 case "ruby":
61 assert_not_reached ();
63 /* Vala offers a feature called verbatim strings. These are strings in
64 * which escape sequences (such as \n) won't be interpreted, line breaks
65 * will be preserved and quotation marks don't have to be masked. They are
66 * enclosed with triple double quotation marks. Possible indentations
67 * after a line break are part of the string as well. Note that syntax
68 * highlighting in this Wiki is not aware of verbatim strings.
70 string verbatim = """This is a so-called "verbatim string".
71 Verbatim strings don't process escape sequences, such as \n, \t, \\, etc.
72 They may contain quotes and may span multiple lines.""";
73 println (verbatim);
74 /* You can apply various operations on strings. Here's a small selection: */
75 println ("from lower case to upper case".up ());
76 println ("reversed string".reverse ());
77 println ("...substring...".substring (3, 9));
78 /* The 'in' keyword is syntactic sugar for checking if one string contains
79 * another string. The following expression is identical to
80 * "swordfish".contains ("word")
82 if ("word" in "swordfish") {
83 // ...
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);