Filter löschen
Filter löschen

Comparing strings with and without ' '

2 Ansichten (letzte 30 Tage)
Mini
Mini am 23 Mär. 2013
I have two strings but one is 'Word' stored with the quotes '' in a cell array, whereas another is a string that is just : Word without the ''. When I do a string compare, it fails. Is there a solution to this?

Antworten (2)

Sven
Sven am 23 Mär. 2013
Bearbeitet: Sven am 23 Mär. 2013
Hi Mini, first I'll make some strings
myStrings = {'''Word''', 'Word'};
>> myStrings{1}
ans =
'Word'
>> myStrings{2}
ans =
Word
Now, I'll do a string compare:
>> strcmp('Word', myStrings)
ans =
0 1
>> strcmp('''Word''', myStrings)
ans =
1 0
>> strcmp('Word', strrep(myStrings,'''',''))
ans =
1 1
Note that they all "work" (ie, they don't error). Also note how I replaced the quote character with the empty string to make the strcmp return true for both versions.
Does this answer your question?
If not, please show us exactly the code to create your two strings and the comparison you are trying to run. Often this is clearer than a sentence describing code.
  3 Kommentare
per isakson
per isakson am 24 Mär. 2013
Most likely the string isn't the same. Show something the way I did below.
Sven
Sven am 25 Mär. 2013
'Word' with a 1x11 char string Word
There are only 4 characters in "Word", or 6 in "'Word'".
Maybe use strtrim() on your variables to get rid of spaces?
You'll note that we can only say things like "maybe" because you keep describing variables rather than just giving code that produces the variable.

Melden Sie sich an, um zu kommentieren.


per isakson
per isakson am 23 Mär. 2013
Bearbeitet: per isakson am 23 Mär. 2013
I'm guessing
>> cac={'Word'}
cac =
'Word'
>> str='Word'
str =
Word
>> strcmp( str, cac )
ans =
1
>>
or less good
>> all( cac{:}==str )
ans =
1

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by