1 void println (string str
) {
2 stdout
.printf ("%s\n", str
);
5 /* Strings are of data type 'string' and can be concatenated with the plus
6 * operator resulting in a new string:
8 string a
= "Concatenated ";
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
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
);
31 /* Strings prefixed with '@' are string templates. They can evaluate
32 * embedded variables and expressions prefixed with '$'.
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.
44 println ("String == operator compares content, not reference.");
46 assert_not_reached ();
48 /* You can compare strings lexicographically with the < and > operators: */
49 if ("blue" < "red" && "orange" > "green") {
56 assert_not_reached ();
58 println ("Switch statement works fine with strings.");
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.""";
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") {
85 // Regular expressions
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
);