7 Using the Strings utility
The strings utility defines some handy functions for dealing with strings.
Strings in C are problematic: they involve a lot of pointer manipulation which
is often complicated, error-prone and a source of bugs. The strings utility
defines a wrapper type for strings (only for convenience), which is defined as:
The functions of the strings utility, as defined in "strings.h", are:
int indexof(str string, char chr);
int nindexof(char chr);
int lindexof(str string, char chr);
str substr(str string, int start);
str nsubstr(str string, int start, int length);
str ltrim(str string);
str rtrim(str string);
str trim(str string);
str toupper(str string);
str tolower(str string);
What the functions do is as following:
-
The indexof() function returns the zero-based index of the first occurence of
’chr’ in ’string’.
-
The nindexof() function returns the zero-based index of the next occurence of
’chr’ in ’string’. It should be called after a previous call the indexof().
-
The lindexof() function returns the zero-based index of the last occurence of
’chr’ in ’string’. If there is only one occurence of ’chr’ in ’string’, the
return value is essentially the same as that of indexof().
-
The substr() function returns a substring of ’string’ starting from position
’start’. Note start is zero-based.
-
The nsubstr() function returns a substring of ’string’ starting from position
’start’ and spanning ’length’ characters. Note start is zero-based.
-
The ltrim() function trims (removes) all the whitespace characters from the
strings’ left side. Whitespace characters removed are: space, tab, and newline.
If there are no whitespace characters in the lefthand side of the string, the
original string is returned.
-
The rtrim() function trims (removes) all the whitespace characters from the
strings’ right side. Whitespace characters removed are: space, tab, and
newline. If there are no whitespace characters in the lefthand side of the
string, the original string is returned.
-
The trim() function trims (removes) all the whitespace characters from both
strings’ ends. Whitespace characters removed are: space, tab, and newline.
If there are no whitespace characters in either side of the string, the
original string is returned.
-
The toupper() function returns the string in upper case letters.
-
The tolower() function returns the string in lower case letters.