Skip to content

Commit ae6ba6f

Browse files
authoredFeb 16, 2025··
docs: add string sample (#70)
1 parent 089014e commit ae6ba6f

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
String Sample
2+
=============
3+
4+
.. literalinclude:: assets/string-sample.vala
5+
:language: vala
6+
7+
Compile and Run
8+
9+
.. code-block:: bash
10+
11+
$ valac string-sample.vala
12+
$ ./string-sample

‎source/tutorials/programming-language/main/02-00-basics/02-04-data-types.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ In addition the *in* operation can be used to determine whether one string conta
122122
123123
For more information, please report to `the complete overview of the string class <http://www.valadoc.org/glib-2.0/string.html>`_.
124124

125-
A `sample program <https://wiki.gnome.org/Projects/Vala/StringSample>`_ demonstrating string usage is also available.
125+
A :doc:`sample program </developer-guides/string-sample>` demonstrating string usage is also available.
126126

127127
Arrays
128128
------

0 commit comments

Comments
 (0)
Please sign in to comment.