Why won't my two strings compare correctly?
조회 수: 14 (최근 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!