Strings

When performing numeric computations, strings are not normally used for much except error messages and such. However, there are many other tasks for which strings are quite useful.

String objects can be treated in a manner similar to numeric objects, with the main difference being the different string operators. The only numeric operator that works on strings is the `+' which catenates the left and right string operands together.

> "first part of the string " + "and the second part of the string"
first part of the string and the second part of the string

The other numeric operators will only produce a runtime error message when used with strings.

> "string1" * "string2"
rlab: NULL, NULL, 1st arg invalid for multiply

The relational and logical operator do work with string objects. For instance, you can compare two strings:

> "string-a" == "string-b"
        0

Or two string matrices:

> ["el-1", "el-2"] == ["el-1", "el-x"]
        1          0

Functions exist to aid with string to number conversions (num2str), and reading strings from the standard input (input).

> x = input ("input a string > ", "s")
input a string > teststring
teststring

The function strsplt will split an arbitrary string into a matrix of one-character strings. This allows the user to operate on individual characters in a string; the `+' operator can be used to ``glue'' the desired pieces back together again.

> x = "this is a single string";
> strsplt (x)
t  h  i  s     i  s     a     s  i  n  g  l  e     s  t  r  i  n  g

We will do a little more with strings in Section [*].