Unlike in C, strings in Nickle are not arrays of or pointers to individual characters. Consistent with its pattern of providing primitive datatypes for types for things that make sense (e.g. file instead of integer file handles), Nickle provides the string type. This has several interesting differences from C-style strings:
In Nickle, strings are immutable--individual characters may not be changed.
Strings are, as with everything else, assigned and passed by-value. See the section on Copy semantics for details.
Two useful operators have been overloaded to allow sane manipulation of strings: '+' and array subscript ('[]').
Although they are not arrays of characters, it is often useful to access a string a character at a time; the array subscript operator has been overloaded to allow this. For example:
Those are the integer representations of each character; they are most likely in ASCII, but not necessarily--see the section on Unicode in the I/O section. The String namespace provides new to recreate a string from these integer character representations, regardless of ASCII or Unicode:
string new(int c)
string new(int[*] cv)
For instance,
On strings, '+' is the concatenation operator. For example,
In addition, the String namespace provides several useful functions that facilitate using strings, including the following.
int index ( string t, string p )
int rindex ( string t, string p )
Returns the index of the first occurence of the substring p in t, or -1 if p is not in t; rindex returns the last occurance instead. For example,
string substr ( string s, int i, int l )
Returns the substring of s which begins at index i and is l characters long. If l is negative, returns the substring of that length which preceeds i instead. For example,