Strings hold an array of bytes or 16bit words depending on the application settings. Normally they are used to store text but can really store any kind of binary data.
There are two types of string constants supported in the AngelScript language, the normal quoted string, and the documentation strings, called heredoc strings.
The normal strings are written between double quotation marks (
") or single quotation marks (
'). Inside the constant strings some escape sequences can be used to write exact byte values that might not be possible to write in your normal editor.
|39||single quotation mark (apostrophe)|
|34||double quotation mark|
|10||new line feed|
|0xFFFF||FFFF should be exchanged for a 1 to 4 digit hexadecimal number representing the value wanted. If the application uses 8bit strings then only values up to 255 is accepted.|
|0xFFFF||FFFF should be exchanged for the hexadecimal number representing the unicode code point|
|0xFFFFFFFF||FFFFFFFF should be exchanged for the hexadecimal number representing the unicode code point|
string str1 = "This is a string with \"escape sequences" ."; string str2 = 'If single quotes are used then double quotes can be included without "escape sequences".';
The heredoc strings are designed for inclusion of large portions of text without processing of escape sequences. A heredoc string is surrounded by triple double-quotation marks (
"""), and can span multiple lines of code. If the characters following the start of the string until the first linebreak only contains white space, it is automatically removed by the compiler. Likewise if the characters following the last line break until the end of the string only contains white space this is also removed.
string str = """ This is some text without "escape sequences". This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. """;
If more than one string constants are written in sequence with only whitespace or comments between them the compiler will concatenate them into one constant.
string str = "First line.\n" "Second line.\n" "Third line.\n";
The escape sequences \u and \U will add the specified unicode code point as a UTF-8 or UTF-16 encoded sequence depending on the application settings. Only valid unicode 5.1 code points are accepted, i.e. code points between U+D800 and U+DFFF (reserved for surrogate pairs) or above U+10FFFF are not accepted.
The string object supports a number of operators, and has several class methods and supporting global functions to facilitate the manipulation of strings.
The assignment operator copies the content of the right hand string into the left hand string.
Assignment of primitive types is allowed, which will do a default transformation of the primitive to a string.
+, += concatenation
The concatenation operator appends the content of the right hand string to the end of the left hand string.
Concatenation of primitives types is allowed, which will do a default transformation of the primitive to a string.
==, != equality
Compares the content of the two strings.
<, >, <=, >= comparison
Compares the content of the two strings. The comparison is done on the byte values in the strings, which may not correspond to alphabetical comparisons for some languages.
 index operator
The index operator gives access to a single byte in the string.
uint length() const
Returns the length of the string.
Sets the length of the string.
bool isEmpty() const
Returns true if the string is empty, i.e. the length is zero.
string substr(uint start = 0, int count = -1) const
Returns a string with the content starting at start and the number of bytes given by count. The default arguments will return the whole string as the new string.
int findFirst(const string &in str, uint start = 0) const
Find the first occurrence of the value str in the string, starting at start. If no occurrence is found a negative value will be returned.
int findLast(const string &in str, int start = -1) const
Find the last occurrence of the value str in the string. If start is informed the search will begin at that position, i.e. any potential occurrence after that position will not be searched. If no occurrence is found a negative value will be returned.
array<string>@ split(const string &in delimiter) const
Splits the string in smaller strings where the delimiter is found.
string join(const array<string> &in arr, const string &in delimiter)
Concatenates the strings in the array into a large string, separated by the delimiter.
int64 parseInt(const string &in str, uint base = 10, uint &out byteCount = 0)
Parses the string for an integer value. The base can be 10 or 16 to support decimal numbers or hexadecimal numbers. If byteCount is provided it will be set to the number of bytes that were considered as part of the integer value.
double parseFloat(const string &in, uint &out byteCount = 0)
Parses the string for a floating point value. If byteCount is provided it will be set to the number of bytes that were considered as part of the value.
string formatInt(int64 val, const string &in options = '', uint width = 0)
string formatUInt(uint64 val, const string &in options = '', uint width = 0)
string formatFloat(double val, const string &in options = '', uint width = 0, uint precision = 0)
The format functions takes a string that defines how the number should be formatted. The string is a combination of the following characters:
// Left justify number in string with 10 characters string justified = formatInt(number, 'l', 10);
// Create hexadecimal representation with capital letters, right justified string hex = formatInt(number, 'H', 10);
// Right justified, padded with zeroes and two digits after decimal separator string num = formatFloat(number, '0', 8, 2);