<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261612</link>
    <title>MATLAB Central Newsreader - Compare strings of different lengths without using strcmp?</title>
    <description>Feed for thread: Compare strings of different lengths without using strcmp?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 24 Sep 2009 20:09:00 -0400</pubDate>
      <title>Compare strings of different lengths without using strcmp?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261612#682476</link>
      <author>NouveauIX</author>
      <description>I'm trying to compare two strings with if statements without using strcmp.&lt;br&gt;
&lt;br&gt;
function log=myStrCmpI(char1,char2)&lt;br&gt;
c1=lower(char1);&lt;br&gt;
c2=lower(char2);&lt;br&gt;
if ?&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;log=true;&lt;br&gt;
else log=false;&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
I tried taking the sum of c1-c2 but that doesn't work for all cases, such as if the strings are the same but have a couple letters switched around.</description>
    </item>
    <item>
      <pubDate>Thu, 24 Sep 2009 20:10:15 -0400</pubDate>
      <title>Re: Compare strings of different lengths without using strcmp?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261612#682479</link>
      <author>dpb</author>
      <description>NouveauIX wrote:&lt;br&gt;
&amp;gt; I'm trying to compare two strings with if statements without using strcmp.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function log=myStrCmpI(char1,char2)&lt;br&gt;
&amp;gt; c1=lower(char1);&lt;br&gt;
&amp;gt; c2=lower(char2);&lt;br&gt;
&amp;gt; if ?&lt;br&gt;
&amp;gt;     log=true;&lt;br&gt;
...&lt;br&gt;
Well, one has to start w/ a clear definition of what the criterion for &lt;br&gt;
equality would be.  If the two aren't the same length, then they &lt;br&gt;
obviously can't be identical so what would &quot;true&quot; be indicating, anyway?&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Thu, 24 Sep 2009 20:16:19 -0400</pubDate>
      <title>Re: Compare strings of different lengths without using strcmp?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261612#682481</link>
      <author>dpb</author>
      <description>dpb wrote:&lt;br&gt;
...&lt;br&gt;
&amp;gt; Well, one has to start w/ a clear definition of what the criterion for &lt;br&gt;
&amp;gt; equality would be.  If the two aren't the same length, then they &lt;br&gt;
&amp;gt; obviously can't be identical so what would &quot;true&quot; be indicating, anyway?&lt;br&gt;
&lt;br&gt;
OBTW, if they are the same length,&lt;br&gt;
&lt;br&gt;
flg = all(s2==s1);&lt;br&gt;
&lt;br&gt;
If they're not, again, &quot;Houston, we have a dimensions problem&quot;...&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Fri, 25 Sep 2009 05:28:52 -0400</pubDate>
      <title>Re: Compare strings of different lengths without using strcmp?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261612#682553</link>
      <author>NouveauIX</author>
      <description>Oh I'm sorry. They need to be equal disregarding case. So 'banana' and 'BaNana' would be equal, as would 'purple' and 'purple'. Result is true if the strings are equal, false if not. I need to be able to enter strings of lengths that aren't equal, though, even if the result will be false.</description>
    </item>
    <item>
      <pubDate>Fri, 25 Sep 2009 07:32:04 -0400</pubDate>
      <title>Re: Compare strings of different lengths without using strcmp?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261612#682570</link>
      <author>Jan Simon</author>
      <description>Dear NouveauIX!&lt;br&gt;
&lt;br&gt;
&amp;gt; I'm trying to compare two strings with if statements without using strcmp.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function log=myStrCmpI(char1,char2)&lt;br&gt;
&amp;gt; c1=lower(char1);&lt;br&gt;
&amp;gt; c2=lower(char2);&lt;br&gt;
&amp;gt; if ?&lt;br&gt;
&amp;gt;     log=true;&lt;br&gt;
&amp;gt; else log=false;&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;function Log = myStrCmpI(char1,char2)&lt;br&gt;
&amp;nbsp;&amp;nbsp;Log = (numel(char1) == numel(char2)) &amp;&amp; ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;all(lower(char1) == lower(char2));&lt;br&gt;
&amp;nbsp;&amp;nbsp;return;&lt;br&gt;
&lt;br&gt;
1. The &amp;&amp; operator performs short-circuiting: the 2nd part of the comparison is not evaluated, if the 1st is FALSE already. This is important.&lt;br&gt;
2. I've renamed &quot;log&quot; to &quot;Log&quot; to reduce conflicts with Matlab's logarithm function. Although this is not an error, it is worth to avoid as many confusions as possible...&lt;br&gt;
&lt;br&gt;
Finally: STRCMPI is really fast and does check the types of the inputs. This can be a tremendous help for debugging! In addition STRCMPI works for cell strings also and cares about CHAR arrays with more than 1 row.&lt;br&gt;
&lt;br&gt;
Good luck, Jan</description>
    </item>
    <item>
      <pubDate>Fri, 25 Sep 2009 17:41:06 -0400</pubDate>
      <title>Re: Compare strings of different lengths without using strcmp?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/261612#682700</link>
      <author>Loren Shure</author>
      <description>In article &lt;br&gt;
&amp;lt;1257816834.5891.1253856562126.JavaMail.root@gallium.mathforum.org&amp;gt;, &lt;br&gt;
visualxd@gmail.com says...&lt;br&gt;
&amp;gt; Oh I'm sorry. They need to be equal disregarding case. So 'banana' and 'BaNana' would be equal, as would 'purple' and 'purple'. Result is true if the strings are equal, false if not. I need to be able to enter strings of lengths that aren't equal, though, even if the result will be false.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Have you looked at strcmpi - which allows you to compare strings in a &lt;br&gt;
case-insensitive manner.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Loren&lt;br&gt;
&lt;a href=&quot;http://blogs.mathworks.com/loren&quot;&gt;http://blogs.mathworks.com/loren&lt;/a&gt;</description>
    </item>
  </channel>
</rss>

