7.7. String
A string
is fundamentally a vector of character
. However, there exists
several differences between the two types: an extra declaration style, an extra literal style, the
result of a concatenation and behaviour when
sent to an output stream.
7.7.1. Declaration
A string may be declared with the keyword string
. The same rules of
vector declarations also apply to strings, allowing
for both explicit and inferred size declarations:
string[*] <identifier> = <type-string>;
string[int-expr] <identifier> = <type-string>;
However, string
variables have an extra method of writing an inferred size
declaration:
string <identifier> = <type-string>;
7.7.2. Null
Same behaviour as null
for vectors. The string is filled with null
characters.
7.7.3. Identity
Same behaviour as identity
for vectors. The string is filled with
identity
characters.
7.7.4. Literals
Strings can be constructed in the same way as vectors using character literals. Gazprea also provides a special syntax for string literals. A string literal is any sequence of character literals (including escape sequences) in between double quotes. For instance:
string cats_meow = "The cat said \"Meow!\"\nThat was a good day.\n"
7.7.5. Operations
Strings have all of the same operations defined on them as the other vector data
types, but with one extra addition. Because a string
and vector of
character
are fundamentally the same, the concatenation operation may be
used to concatenate values of the two types. As well, a scalar character
may
be concatenated onto a string
in the same way as it would be concatenated
onto a vector of character
.
This operation should always result in a value with type string
. Again,
because a string
is always able to be converted to a vector of
character
, this is only apparent when printing the result. For example:
['a', 'b'] || "cd" -> std_output;
"ef" || 'g' -> std_output;
prints the following:
abcdefg
7.7.6. Type Casting and Type Promotion
To see the types that string
may be cast and/or promoted to, see the
sections on Type Casting and Type Promotion respectively.