Index and substring

A string can be used as if it were a one-dimensional array of characters. That is, an individual character or a substring can be accessed and modified by using an index expression.

>> x = "The flying pig"
   The flying pig
>> x[3:5]
   e f
>> x[1] = "E";
   Ehe flying pig
>> x[1:3] = "the";
   the flying pig
>> x[$-2:$] = "wig"
   the flying wig
Note that the $ sign when appearing in an index expression represents the length of the variable being indexed, therefore x[$] refers to the last character of the string.

ASCII values can be assigned to one or several characters of a string. For example,

>> x = "The flying pig"
>> x[12] = 119;
>> x
    The flying wig
>> x[12:14] = [98, 112, 103];
>> x
    The flying bug"
Note that the value assigned to string must be valid ASCII value; in particular, zero value is not allowed.



oz 2009-12-22