Strings are scalar immutable objects. Strings are declared as follows:
string message = "Hello";String concatenation:
string username = "User";
string new_message = message ~ " " ~ username;When concatenating strings, a new object is created. Therefore, if you need to join multiple strings, you should use a Vector:
Vector messages = [];
string message = rs::join("", messages);
print(message);The Runtime.rs library is used for working with strings.
To get the length of a string, use strlen:
int rs::strlen(string s);string rs::substr(string s, int start, int len);s – the string
start – starting position
len – length of the new substring
char rs::charAt(string s, string pos);s – the string
pos – index of the character
int rs::chr(int code);code – ASCII code
int rs::ord(char ch);ch – character
string rs::lower(string s);s – the string
string rs::upper(string s);s – the string
int rs::compare(string a, string b);a, b – strings to compare
Example usage:
Vector arr = [
"apple",
"banana",
"orange"
];
arr.sort(int (string a, string b) => rs::compare(a, b));int rs::replace(string search, string item, string s);search – string to find
item – replacement string
s – source string
string rs::str_repeat(string s, int n);s – the string
n – number of repetitions
string rs::split(string delimiter, string s, int limit = -1)delimiter – separator
s – the string
limit – maximum number of parts
Vector<string> rs::splitArr(Vector<string> delimiters, string s, int limit = -1);delimiters – list of separators
s – the string
limit – maximum number of parts
string rs::join(string ch, Vector<string> arr);ch – separator
arr – array of strings
string rs::join_path(Vector arr);arr – array representing path components
Example:
string path = rs::join_path(["folder", "name.txt"]);string rs::trim(string s, string ch = "");s – the string
ch – characters to trim (default removes whitespace)
int rs::indexOf(string s, string search, string offset = 0);s – the string to search in
search – substring to find
offset – position to start searching
string rs::format(string s, Dict params = null);s – format string
params – parameters dictionary
Example:
string message = rs::format("Hello %username%", {"username": "User"});