<html lang="en"> <head> <title>Comparing Strings - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Strings.html#Strings" title="Strings"> <link rel="prev" href="Creating-Strings.html#Creating-Strings" title="Creating Strings"> <link rel="next" href="Manipulating-Strings.html#Manipulating-Strings" title="Manipulating Strings"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Comparing-Strings"></a> <p> Next: <a rel="next" accesskey="n" href="Manipulating-Strings.html#Manipulating-Strings">Manipulating Strings</a>, Previous: <a rel="previous" accesskey="p" href="Creating-Strings.html#Creating-Strings">Creating Strings</a>, Up: <a rel="up" accesskey="u" href="Strings.html#Strings">Strings</a> <hr> </div> <h3 class="section">5.4 Comparing Strings</h3> <p>Since a string is a character array, comparisons between strings work element by element as the following example shows: <pre class="example"> GNU = "GNU's Not UNIX"; spaces = (GNU == " ") ⇒ spaces = 0 0 0 0 0 1 0 0 0 1 0 0 0 0 </pre> <p class="noindent">To determine if two strings are identical it is necessary to use the <code>strcmp</code> function. It compares complete strings and is case sensitive. <code>strncmp</code> compares only the first <code>N</code> characters (with <code>N</code> given as a parameter). <code>strcmpi</code> and <code>strncmpi</code> are the corresponding functions for case-insensitive comparison. <!-- strcmp src/strfns.cc --> <p><a name="doc_002dstrcmp"></a> <div class="defun"> — Built-in Function: <b>strcmp</b> (<var>s1, s2</var>)<var><a name="index-strcmp-349"></a></var><br> <blockquote><p>Return 1 if the character strings <var>s1</var> and <var>s2</var> are the same, and 0 otherwise. <p>If either <var>s1</var> or <var>s2</var> 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. <p><strong>Caution:</strong> For compatibility with <span class="sc">matlab</span>, 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. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dstrcmpi.html#doc_002dstrcmpi">strcmpi</a>, <a href="doc_002dstrncmp.html#doc_002dstrncmp">strncmp</a>, <a href="doc_002dstrncmpi.html#doc_002dstrncmpi">strncmpi</a>. </p></blockquote></div> <!-- strncmp src/strfns.cc --> <p><a name="doc_002dstrncmp"></a> <div class="defun"> — Built-in Function: <b>strncmp</b> (<var>s1, s2, n</var>)<var><a name="index-strncmp-350"></a></var><br> <blockquote><p>Return 1 if the first <var>n</var> characters of strings <var>s1</var> and <var>s2</var> are the same, and 0 otherwise. <pre class="example"> strncmp ("abce", "abcd", 3) ⇒ 1 </pre> <p>If either <var>s1</var> or <var>s2</var> 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. <pre class="example"> strncmp ("abce", {"abcd", "bca", "abc"}, 3) ⇒ [1, 0, 1] </pre> <p><strong>Caution:</strong> For compatibility with <span class="sc">matlab</span>, 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. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dstrncmpi.html#doc_002dstrncmpi">strncmpi</a>, <a href="doc_002dstrcmp.html#doc_002dstrcmp">strcmp</a>, <a href="doc_002dstrcmpi.html#doc_002dstrcmpi">strcmpi</a>. </p></blockquote></div> <!-- strcmpi src/strfns.cc --> <p><a name="doc_002dstrcmpi"></a> <div class="defun"> — Built-in Function: <b>strcmpi</b> (<var>s1, s2</var>)<var><a name="index-strcmpi-351"></a></var><br> <blockquote><p>Return 1 if the character strings <var>s1</var> and <var>s2</var> are the same, disregarding case of alphabetic characters, and 0 otherwise. <p>If either <var>s1</var> or <var>s2</var> 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. <p><strong>Caution:</strong> For compatibility with <span class="sc">matlab</span>, 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. <p><strong>Caution:</strong> National alphabets are not supported. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dstrcmp.html#doc_002dstrcmp">strcmp</a>, <a href="doc_002dstrncmp.html#doc_002dstrncmp">strncmp</a>, <a href="doc_002dstrncmpi.html#doc_002dstrncmpi">strncmpi</a>. </p></blockquote></div> <!-- strncmpi src/strfns.cc --> <p><a name="doc_002dstrncmpi"></a> <div class="defun"> — Built-in Function: <b>strncmpi</b> (<var>s1, s2, n</var>)<var><a name="index-strncmpi-352"></a></var><br> <blockquote><p>Return 1 if the first <var>n</var> character of <var>s1</var> and <var>s2</var> are the same, disregarding case of alphabetic characters, and 0 otherwise. <p>If either <var>s1</var> or <var>s2</var> 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. <p><strong>Caution:</strong> For compatibility with <span class="sc">matlab</span>, 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. <p><strong>Caution:</strong> National alphabets are not supported. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dstrncmp.html#doc_002dstrncmp">strncmp</a>, <a href="doc_002dstrcmp.html#doc_002dstrcmp">strcmp</a>, <a href="doc_002dstrcmpi.html#doc_002dstrcmpi">strcmpi</a>. </p></blockquote></div> <!-- validatestring scripts/strings/validatestring.m --> <p><a name="doc_002dvalidatestring"></a> <div class="defun"> — Function File: <var>validstr</var> = <b>validatestring</b> (<var>str, strarray</var>)<var><a name="index-validatestring-353"></a></var><br> — Function File: <var>validstr</var> = <b>validatestring</b> (<var>str, strarray, funcname</var>)<var><a name="index-validatestring-354"></a></var><br> — Function File: <var>validstr</var> = <b>validatestring</b> (<var>str, strarray, funcname, varname</var>)<var><a name="index-validatestring-355"></a></var><br> — Function File: <var>validstr</var> = <b>validatestring</b> (<var><small class="dots">...</small>, position</var>)<var><a name="index-validatestring-356"></a></var><br> <blockquote><p>Verify that <var>str</var> is an element, or substring of an element, in <var>strarray</var>. <p>When <var>str</var> is a character string to be tested, and <var>strarray</var> is a cellstr of valid values, then <var>validstr</var> will be the validated form of <var>str</var> where validation is defined as <var>str</var> being a member or substring of <var>validstr</var>. This is useful for both verifying and expanding short options, such as "r", to their longer forms, such as "red". If <var>str</var> is a substring of <var>validstr</var>, 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 <var>str</var> is ambiguous. All comparisons are case insensitive. <p>The additional inputs <var>funcname</var>, <var>varname</var>, and <var>position</var> are optional and will make any generated validation error message more specific. <p>Examples: <!-- Set example in small font to prevent overfull line --> <pre class="smallexample"> validatestring ("r", {"red", "green", "blue"}) ⇒ "red" validatestring ("b", {"red", "green", "blue", "black"}) ⇒ error: validatestring: multiple unique matches were found for 'b': blue, black </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dstrcmp.html#doc_002dstrcmp">strcmp</a>, <a href="doc_002dstrcmpi.html#doc_002dstrcmpi">strcmpi</a>. </p></blockquote></div> </body></html>