﻿ Strings And String Operations
 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.

 Operator Description str1 == str2 Checks for the equality of two strings. str1 != str2 Checks for the inequality of two strings. str1 < str2 Checks if the first string is less than the second string. str1 <= str2 Checks if the first string is less or equal to the second string. str1 > str2 Checks if the first string is greater than the other string. str1 >= str2 Checks if the first string is greater or equal to the other string.

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;
//is also valid to check for letters and other characters

...

}

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

 Function Description int findstring(int startIndex, string text, string substring) This functions looks for the substring in the given text. The search starts at the given startIndex. The first character has an index of 0. The function starts its search at a specified position of the entire text using startIndex and returns an index that describes the position at which the substring begins. If the substring is not found, -1 is returned. string substring(string text, int startIndex, int count) The function extracts count characters from the given text starting with startIndex. If count is -1, all characters of the string starting at startIndex are returned. c int rfindstring(int startIndex, string text, string substring) This functions looks for the substring in the given text from its end to the beginning. The function starts its search at a specified position of the entire text using startIndex and returns an index that describes the position at which the substring begins. If the substring was not found, -1 is returned. int startswith(string text, string substring) This function checks if the text string starts with the given substring. If text starts with substring, true is returned, otherwise false. int endswith(string text, string substring) This function checks if the text string ends with the given substring. If text ends with substring, true is returned, otherwise false. int isalnum(string text) Returns true if the given string contains only characters and figures and its length is greater then 0, otherwise false is returned. int isalpha(string text) Returns true if the given string contains only characters and its length is greater than 0, otherwise false is returned. int isnum(string text) Returns true if the given text represents a number. This may be an integer number or a floating point number (e.g.  1.3). Otherwise it returns false. void tolower(string text) Converts each character of the given string into a lower-case character. void toupper(string text) Converts each character of the given string into an upper-case character. void strip(string text) Removes leading and ending white spaces like space, tabulator, line feeds and so on from the given string. int strcmp(string str1, string str2) Compares two given strings with each other. If they are equal, 0 is returned. -1 is returned if str1 is less than str2. A value of 1 is returned if str1 is bigger than str2. void replace(string src, string old, string new) Replaces any appearances of old within src with new. void tokenize(string src, string delimiter, string reslist[]) Separates the string src into smaller pieces delimited by characters within delemiter. The result is returned in reslist. See below for further details.

Tokenizing 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:

 ▪ 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 coma but not by space.

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

Substring

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.6i | Script Version: 2.21 [Ctrl & +/-] = Zoom In/Out | [Ctrl & 0] = 100% Print   Previous   Next