- publishing free software manuals
GNU Octave Manual Version 3
by John W. Eaton, David Bateman, Søren Hauberg
Paperback (6"x9"), 568 pages
ISBN 095461206X
RRP £24.95 ($39.95)

Get a printed copy>>>

5.2 Comparing Strings

Since a string is a character array, comparisons between strings work element by element as the following example shows:

GNU = "GNU's Not UNIX";
spaces = (GNU == " ")
=> spaces =
      0   0   0   0   0   1   0   0   0   1   0   0   0   0

To determine if two strings are identical it is therefore necessary to use the strcmp or strncpm functions. Similar functions exist for doing case-insensitive comparisons.

Built-in Function: strcmp (s1, s2)
Return 1 if the character strings s1 and s2 are the same, and 0 otherwise.

If either s1 or s2 is a cell array of strings, then an array of the same size is returned, containing the values described above for every member of the cell array. The other argument may also be a cell array of strings (of the same size or with only one element), char matrix or character string.

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

See also strcmpi, strncmp, strncmpi

Function File: strcmpi (s1, s2)
Ignoring case, return 1 if the character strings s1 and s2 are the same, and 0 otherwise.

If either s1 or s2 is a cell array of strings, then an array of the same size is returned, containing the values described above for every member of the cell array. The other argument may also be a cell array of strings (of the same size or with only one element), char matrix or character string.

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

See also strcmp, strncmp, strncmpi

Built-in Function: strncmp (s1, s2, n)
Return 1 if the first n characters of strings s1 and s2 are the same, and 0 otherwise.

strncmp ("abce", "abcd", 3)
     => 1

If either s1 or s2 is a cell array of strings, then an array of the same size is returned, containing the values described above for every member of the cell array. The other argument may also be a cell array of strings (of the same size or with only one element), char matrix or character string.

strncmp ("abce", {"abcd", "bca", "abc"}, 3)
     => [1, 0, 1]

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

See also strncmpi, strcmp, strcmpi

Function File: strncmpi (s1, s2, n)
Ignoring case, return 1 if the first n characters of character strings s1 and s2 are the same, and 0 otherwise.

If either s1 or s2 is a cell array of strings, then an array of the same size is returned, containing the values described above for every member of the cell array. The other argument may also be a cell array of strings (of the same size or with only one element), char matrix or character string.

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

See also strcmp, strcmpi, strncmp

ISBN 095461206XGNU Octave Manual Version 3See the print edition