1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
<html lang="en">
<head>
<title>Comparing Strings - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<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">
<p>
<a name="Comparing-Strings"></a>
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.
<!-- 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-304"></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>
<!-- 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-305"></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>
<!-- ./strings/strcmpi.m -->
<p><a name="doc_002dstrcmpi"></a>
<div class="defun">
— Function File: <b>strcmpi</b> (<var>s1, s2</var>)<var><a name="index-strcmpi-306"></a></var><br>
<blockquote><p>Ignoring case, return 1 if the character strings (or character
arrays) <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 strcmpi
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_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>
<!-- ./strings/strncmpi.m -->
<p><a name="doc_002dstrncmpi"></a>
<div class="defun">
— Function File: <b>strncmpi</b> (<var>s1, s2, n</var>)<var><a name="index-strncmpi-307"></a></var><br>
<blockquote><p>Ignoring case, return 1 if the first <var>n</var> characters of character
strings (or character arrays) <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 strncmpi
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_002dstrcmp.html#doc_002dstrcmp">strcmp</a>, <a href="doc_002dstrcmpi.html#doc_002dstrcmpi">strcmpi</a>, <a href="doc_002dstrncmp.html#doc_002dstrncmp">strncmp</a>.
</p></blockquote></div>
<!-- ./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-308"></a></var><br>
— Function File: <var>validstr</var> = <b>validatestring</b> (<var>str, strarray, funcname</var>)<var><a name="index-validatestring-309"></a></var><br>
— Function File: <var>validstr</var> = <b>validatestring</b> (<var>str, strarray, funcname, varname</var>)<var><a name="index-validatestring-310"></a></var><br>
— Function File: <var>validstr</var> = <b>validatestring</b> (<var><small class="dots">...</small>, position</var>)<var><a name="index-validatestring-311"></a></var><br>
<blockquote><p>Verify that <var>str</var> is a string or substring of an element of
<var>strarray</var>.
<p><var>str</var> is a character string to be tested, and <var>strarray</var> is a
cellstr of valid values. <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>. 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, and an
error will be raised if the matches are not substrings of each other.
<p>All comparisons are case insensitive.
<!-- 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>
|