Go to the first, previous, next, last section, table of contents.

String Functions

Octave currently has a limited ability to work with strings.

The function strcmp (s1, s2) compares two strings, returning 1 if they are the same, and 0 otherwise.

Note: For compatibility with MATLAB, Octave's strcmp function returns 1 if the strings are equal, and 0 otherwise. This is just the opposite of the corresponding C library function.

The functions int2str and num2str convert a numeric argument to a string. These functions are not very flexible, but are provided for compatibility with MATLAB. For better control over the results, use sprintf (see section Formatted Output).

The function setstr can be used to convert a vector to a string. Each element of the vector is converted to the corresponding ASCII character. For example,

setstr ([97, 98, 99])

creates the string

abc

The function undo_string_escapes (string) converts special characters in strings back to their escaped forms. For example, the expression

bell = "\a";

assigns the value of the alert character (control-g, ASCII code 7) to the string variable bell. If this string is printed, the system will ring the terminal bell (if it is possible). This is normally the desired outcome. However, sometimes it is useful to be able to print the original representation of the string, with the special characters replaced by their escape sequences. For example,

octave:13> undo_string_escapes (bell)
ans = \a

replaces the unprintable alert character with its printable representation. See section String Constants, for a description of string escapes.


Go to the first, previous, next, last section, table of contents.