Why won't my two strings compare correctly?
12 ビュー (過去 30 日間)
古いコメントを表示
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.
回答 (2 件)
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
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
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
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 件のコメント
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 Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!