Strings And String Operations |
Print Previous Next |
Introduction MADRIX Script provides several functions to manipulate strings, find substrings, and to perform many more operations.
Operations On Strings Assigning Data Types It is possible to assign integer, float, and strings to another string as shown in the following example: string s; string t; s = t; s = "Hello world"; s = 5; s = 3.5;
Furthermore, it is possible to assign single characters of a string to a character of another string like shown below: string s, t; s = "New"; t = "new";
s = t; s[0] = t[0];
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; s[0] = "T"; s[1] = "1"; s[2] = ".";
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 character but not one s[0] = ""; //given string is empty
Comparing Two Strings As it is possible to compare two numbers using the compare operators, it is also possible to compare two strings. The following table provides an overview of the possible operations.
Please note: It is not possible to compare arrays of strings. Like in the case of assignments, it is also possible to compare single characters of a string against a double-quoted string with exactly one character: if(s[0] == "A") ... else if(s[0] == "!") ... ...
It is also possible to compare single characters of a string against integer numbers: if(s[0] == 1) ... else if(s[0] == 2) ...
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"; switch(s[0]) { case "1": do something; break; case 1: do something else; break; case "A": do something; break; ... }
Using Strings Within Switch/Case Statements Another possibility is to use double-quoted strings of one character for of labels. The following theoretical example demonstrates this: string s = "New"; for(int i = 0; i < s.length; ++i) { switch(s[i]) { case "A": do something; break; case "B": do something else; break; case "!": do something; break; } }
Functions For Strings
The function tokenize() enables you to tokenize a string. 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". string res[]; tokenize(s, " ,", res);
string s = "one two,three"; string res[]; tokenize(s, "," , res);
Explanation:
Splitting Strings With White Spaces There 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". string res[]; tokenize(s, WHITE_SPACES, res);
//or another example which also uses the comma as delimiter tokenize(s, WHITE_SPACES + ",", res);
Examples This 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"; string subText = substring(txt, 6, 5); //retrieves "World" from txt WriteText(subText);
|
MADRIX Version: 3.6j | Script Version: 2.22 |
[Ctrl & +/-] = Zoom In/Out | [Ctrl & 0] = 100% |
Print Previous Next |