Why won't my two strings compare correctly?
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Respuestas (2)
Image Analyst
el 10 de Oct. de 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 comentarios
Walter Roberson
el 10 de Oct. de 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
el 10 de Oct. de 2017
You're right. I'd forgotten about that new feature. I was thinking about character arrays, not the new "string" variables.
Walter Roberson
el 10 de Oct. de 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 comentarios
Walter Roberson
el 10 de Oct. de 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.
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!