Graphics Programs Reference
In-Depth Information
26.2 Comparing Strings
The == test is not a good idea with strings because it compares the
ascii values of the strings, which must have the same length; if the
strings are not the same length, you get an error. The strcmp command
avoids this di@culty:
>> c1 = 'blond';
>> c2 = 'brown';
>> c3= 'blonde';
>> c1 = = c2
ans =
1
0
1
0
0
>> c2 = = c3
??? Array dimensions must match for binary array op.
>> strcmp(c2,c3)
ans =
0
26.3 String Manipulations
Typing help strfun displays the full set of commands for working with
strings. A common example is to identify words within a string by
searching for whitespace (blank characters, tabs, etc.):
>> str = 'I go now';
>> isspace(str)
ans =
0
1
0
0
1
0
0
0
You can also search for letters:
>> isletter(str)
ans =
1
0
1
1
0
1
1
1
To find where a shorter string occurs within a longer one, use the
findstr command:
>> pos = findstr(str,'go')
pos =
3
>> pos = findstr(str,'o')
pos =
4
7
To replace one string with another, use the strrep command:
Search WWH ::




Custom Search