Next: Manipulating Strings, Previous: Creating Strings, Up: Strings [Contents][Index]

### 5.4 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 necessary to use the `strcmp` function. It compares complete strings and is case sensitive. `strncmp` compares only the first `N` characters (with `N` given as a parameter). `strcmpi` and `strncmpi` are the corresponding functions for case-insensitive comparison.

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.

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.

Built-in Function: strcmpi (s1, s2)

  Return 1 if the character strings s1 and s2 are the same, disregarding case of alphabetic characters, 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.

  Caution: National alphabets are not supported.

  See also: strcmp, strncmp, strncmpi.

Built-in Function: strncmpi (s1, s2, n)

  Return 1 if the first n character of s1 and s2 are the same, disregarding case of alphabetic characters, 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.

  Caution: National alphabets are not supported.

  See also: strncmp, strcmp, strcmpi.

Function File: validstr = validatestring (str, strarray)
Function File: validstr = validatestring (str, strarray, funcname)
Function File: validstr = validatestring (str, strarray, funcname, varname)
Function File: validstr = validatestring (…, position)

  Verify that str is an element, or substring of an element, in strarray.

  When str is a character string to be tested, and strarray is a cellstr of valid values, then validstr will be the validated form of str where validation is defined as str being a member or substring of validstr. This is useful for both verifying and expanding short options, such as `"r"`, to their longer forms, such as `"red"`. If str is a substring of validstr, and there are multiple matches, the shortest match will be returned if all matches are substrings of each other. Otherwise, an error will be raised because the expansion of str is ambiguous. All comparisons are case insensitive.

  The additional inputs funcname, varname, and position are optional and will make any generated validation error message more specific.

  Examples:

      validatestring ("r", {"red", "green", "blue"})
      ⇒ "red"

      validatestring ("b", {"red", "green", "blue", "black"})
      ⇒ error: validatestring: multiple unique matches were found for 'b':
         blue, black

  See also: strcmp, strcmpi.

Next: Manipulating Strings, Previous: Creating Strings, Up: Strings [Contents][Index]