Why won't my two strings compare correctly?

12 ビュー (過去 30 日間)
Chris Reischel
Chris Reischel 2017 年 10 月 10 日
コメント済み: Walter Roberson 2017 年 10 月 10 日
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.
  1 件のコメント
per isakson
per isakson 2017 年 10 月 10 日
Did you try
strcmp( namesref, JR )

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2017 年 10 月 10 日
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 件のコメント
Walter Roberson
Walter Roberson 2017 年 10 月 10 日
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 2017 年 10 月 10 日
You're right. I'd forgotten about that new feature. I was thinking about character arrays, not the new "string" variables.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2017 年 10 月 10 日
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 件のコメント
Chris Reischel
Chris Reischel 2017 年 10 月 10 日
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 2017 年 10 月 10 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by