Strings And String Operations |
Previous Next |
IntroductionMADRIX Script provides several functions to manipulate strings, find substrings, and to perform many more operations.
CompatibilityStrings in MADRIX 5 support the UTF-8 encoding standard. That mainly means that a variety of characters of different languages as well as special characters can be used. Several special characters can be masked to make sure that they are included in the text. Otherwise, the script language will interpret them as parts of the language with much different functionality and not part of the text. ▪A \ can be masked with "\\". ▪A " can be masked with "\"". ▪A new line/an end-of-line can be masked with "\n". ▪A tab/tabulator can be masked with "\t". ▪A carriage return can be masked with "\r".
Operations On StringsAssigning Data TypesIt is possible to assign integer, float, and string values to another string as shown in the following example: string s; Furthermore, it is possible to assign a single character of a string to a character of another string like shown below: string s, t; Furthermore, it is possible to assign a double-quoted string to a character of a string. But the assigned string must have exactly one character. Here is an example: string s; The following lines are invalid and will result in a compiler error since the given strings contain more or less than one character: s[0] = "New"; //given string has three characters but not one
Comparing Two StringsAs it is possible to compare two numbers using the comparison operators, it is also possible to compare two strings. The following table provides an overview of the possible operations.
Please note: The sorting order of the strings is case-sensitive and depends on the contained ASCII characters. Therefore, all upper case characters are "less" than the set of lower case characters, e.g. the string array { "and", "Alice", "Bob", "and me" } results in the ascendingly sorted string array { "Alice", "Bob", "and", "and me" }. Like in the case of assignments, it is also possible to compare a single character of a string with a double-quoted string with exactly one character: if(s[0] == "A") ... It is also possible to compare a single character of a string with an integer number: if(s[0] == 1) ... This also works for the switch/case statements. But the "1" as a label of a case means the same as the 1. So the following two case labels mean the same and this would result in a compiler error: string s = "1"; Using Strings Within Switch/Case StatementsAnother possibility is to use double-quoted strings of one character for the labels of cases. The following theoretical example demonstrates this: string s = "New"; Functions For Strings
Tokenizing StringsThe function tokenize enables you to split a string into smaller parts separated by specified delimiters. The single tokens will be delimited by the characters within the second parameter. Each character identifies a single delimiter. The following examples show the usage of the function and the results. string s = "Have a wonderful,nice day";
Explanation: ▪The variable res of the first example will be filled with the following five values: {Have; a; wonderful; nice; day} ▪The res variable of the second example will be filled with the following two values: {one two; three} ▪The result of the second example will contain only two entries. "one two" is only one entry since the tokens of the second example are only delimited by comma but not by space.
Splitting Strings With White SpacesThere is a constant called WHITE_SPACES which can be used as delimiter in order to split a text by any white spaces like tabulator, new line, or space. string s = "Have a wonderful, nice day";
ExamplesSubstringThis example extracts a part from a string. Insert the source code into the function RenderEffect. As the result "World" should be displayed in the output window of the Script Editor. string txt = "Hello World!";
|
MADRIX Version: 5.7 | Script Version: 3.23 |
[Ctrl & +/-] = Zoom In/Out | [Ctrl & 0] = 100% |
Previous
Next
|
Enable automatic translation | Activer la traduction automatique | |