Operators: Index operator []

Syntax

string‑operand[integer‑operand]

array‑operand[integer‑operand]

Access string characters

In its first version, the index [] operator selects a character within the string operand. A string can be considered as an one-dimensional array consisting of characters. Within the string all characters are numbered consecutively starting with 0 for the first character, 1 for the second and so far. Accordingly, the integer operand determines the number of the desired character to select.

Once selected, the character can be evaluated within an expression. Please note, trying to access a character with a negative index or an index equal to or greater than the length of the string will result in a zero character '\0'. For example:

var string a = "Hello World"; var int32 b = 1; var int32 c = a.length; var int32 d = -1; // Read a character var char result1 = a[ b ]; // result1 = 'e' var char result2 = a[ c ]; // result2 = '\0' var char result3 = a[ d ]; // result3 = '\0'

In turn, assigning a value to the selected character will modify the string in-place. The modification of the string results in a new version of the string without the original string being affected. In other words, other references to the original string remain unchanged. Please note, trying to modify a character with a negative index or an index equal to or greater than the length of the string will raise a Chora runtime error. For example:

var string a = "Hello World"; // The variable 'b' refers also to the original string var string b = a; var int32 i; // Convert all lowercase signs in the string 'a' in uppercase for ( i = 0; i < a.length; i = i + 1 ) a[ i ] = a[ i ].upper; // The variable 'a' contains the modified string. The variable 'b' // refers still to the original not modified string. trace a; // "HELLO WORLD" trace b; // "Hello World" var int32 c = a.length; var int32 d = -1; a[ c ] = 'x'; // Runtime error a[ d ] = 'x'; // Runtime error

Please note, the modification of characters within a string by using the index operator is restricted to be performed in context of a locally declared variable only. Trying to modify a string stored e.g. in a property or array will raise a Chora compiler error. If you intend to modify a string stored in e.g. a property, copy the string first in a locally declared variable, then apply the desired modifications on it and finally assign the resulting string back to the original property.

For example, let's assume you want to replace in the string displayed in a Text view every '+' sign by a '-' sign, then following code is appropriate:

// First get the actual string displayed in the affected Text view. // Store the string in a local variable 'str'. var string str = TextView.String; var int32 i; // Loop through the characters in the string and replace '+' by '-'. for ( i = 0; i < str.length; i = i + 1 ) if ( str[ i ] == '+' ) str[ i ] = '-'; // Assign the modified string back to the Text view TextView.String = str;

For other techniques to format strings dynamically at the runtime please see the string instant constructors and the string instant methods. To combine several strings together use the concatenation operator.

Access array elements

When used in context of an array or a local array, the index [] operator selects an items from the array. Within an array all items are numbered consecutively starting with 0 for the first item, 1 for the second and so far. Accordingly, the integer operand determines the number of the desired item to select. Once selected, the item can be evaluated or modified. For example:

array string a[4]; var int32 b = 1; var int32 c = a.size; var int32 d = -1; // Write data to an item of the array a[ b ] = "Hello World"; // Read data from the array var string result = a[ b ]; // result = "Hello World";

Please note, trying to access an array item with a negative index or an index equal to or greater than the size of the array will produce a Chora compiler or runtime error depending on whether the index is a constant well known at the compilation time or a value calculated dynamically at the runtime. For example:

array string a[4]; var int32 c = a.size; var int32 d = -1; a[0] = "Hello"; a[1] = "Hallo"; a[2] = "Hola"; a[3] = "Salut"; a[4] = "Ciao"; // Chora compiler error var string result1 = a[ c ]; // Chora runtime error var string result2 = a[ d ]; // Chora runtime error