Next: , Previous: , Up: Strings   [Contents][Index]


20.3.4 String Concatenation

Strings can be concatenated using the + operator. This works like this:

(poke) "foo" + "bar"
"foobar"

Note how the null character terminating the first string is removed. Therefore, the length of the concatenation of two given strings of lengths N and M is always N+M-1.

Concatenation and indexing are useful together for building strings. A string can be created empty, and additional characters added to it by means of concatenation:

(poke) var bytes = "";
(poke) bytes = bytes + 'x' as string;

Then, we can retrieve characters from the string we built using indexing:

(poke) bytes[0]
0x78UB

Additionally, the * operator allows to “multiply” a string by concatenating it with itself a given number of times. This works like this:

(poke) "foo" * 3
"foofoofoo"
(poke) "foo" * 0
""

This is useful for building strings whose length is not known at compile time. For example:

fun make_empty_string = (int length) string:
{
   return " " * length;
}