Why won't my two strings compare correctly?

12 views (last 30 days)
I previously asked this, and I think I found a better way to ask the question. So I have a string array full of names of every baseball player. I called it namesref. I am trying to index into that string array. Here is my code:
>> namesref(1202)
ans =
"Josh Reddick*"
>> find(namesref == ans)
ans =
1202
>> JR = "Josh Reddick*"
JR =
"Josh Reddick*"
>> find(namesref == JR)
ans =
0×1 empty double column vector
Why does the find function not find the JR string, even though it matches exactly with the ans I was given when I first indexed the namesref matrix? This is really frustrating, ans and JR seem to be the exact same variable yet they are not equal to MATLAB.

Answers (2)

Image Analyst
Image Analyst on 10 Oct 2017
Because, like with most/all other languages, you're supposed to use string functions for that. You should use strcmp() or strcmpi(). You could also use strfind(), contains(), or ismember(). You might want to learn those functions.
When you do namesref == JR, it actually generates a logical vector of 12 boolean values, then find() finds the indexes of that vector that are not equal to zero. This is a completely different thing than what strcmp() does.
  2 Comments
Walter Roberson
Walter Roberson on 10 Oct 2017
IA, notice the double quotes. The poster is already using string operations -- string object operations, for which == between strings is well defined.
Image Analyst
Image Analyst on 10 Oct 2017
You're right. I'd forgotten about that new feature. I was thinking about character arrays, not the new "string" variables.

Sign in to comment.


Walter Roberson
Walter Roberson on 10 Oct 2017
I suspect that if you compare
char(namesref(1202)) + 0
to
char(JR) + 0
that you will find that namesref(1202) contains a character that either displays as empty or as blank. For example the space that shows up might be U+00A0, char(160), non-breaking space, ' ' html coded as
  2 Comments
Chris Reischel
Chris Reischel on 10 Oct 2017
Yes, this occurs. The first four are the same, as they spell out "Josh," yet the space is different. Huh. Good thinking. Now I know what the difference is. Thank you! The space code that I am looking for is 160, yet code 32 is the one present in my variable, how can I change the 32 to the 160?
Walter Roberson
Walter Roberson on 10 Oct 2017
StringVariable = replace(StringVariable, char(32), char(160))
However, I recommend you consider
StringVariable = regexprep(StringVariable, '\s', ' ');
which would replace all forms of whitespace by space, making it unnecessary to worry that some of them might be space and some might be nbsp and some might be thin space, and so on.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!