Strings: A String Is An Array of Characters
Strings: A String Is An Array of Characters
MATLAB Strings
Selim Aksoy
Bilkent University
Department of Computer Engineering
[email protected]
s = 'abc'
is equivalent to s = [ 'a' 'b' 'c' ]
s(1) 'a'
s( [ 1 2 ] ) = 'XX' s = 'XXc'
s(end) 'c'
Fall 2004
String Conversion
n
Fall 2004
CS 111
n
n
String Tests
n
Fall 2004
Fall 2004
0
CS 111
CS 111
String Comparison
Character Arrays
CS 111
Fall 2004
'fate' == 'cake'
ans =
0
1
0
'fate' > 'cake'
ans =
1
0
1
0
CS 111
String Comparison
n
a = 'Bilkent';
strcmp( a, 'Bilkent' )
ans =
1
strcmp( 'Hello', 'hello' )
ans =
0
Fall 2004
CS 111
Searching in Strings
Fall 2004
CS 111
Fall 2004
CS 111
10
11
Fall 2004
String Conversion
CS 111
Replacing in Strings
n
Fall 2004
Searching in Strings
Uppercase-to-lowercase
Lowercase-to-uppercase
Fall 2004
x = str2num( '3.1415' )
x=
3.1415
CS 111
12
String Conversion
n
String Comparison
n
n
Pseudocode:
n
n
n
Fall 2004
CS 111
13
Fall 2004
14
String Comparison
function result = c_strcmp(str1,str2)
%C_STRCMP Compare strings like C function "strcmp"
% Function C_STRCMP compares two strings, and returns
% a -1 of str1 < str2, a 0 if str1 == str2, and a
% +1 if str1 > str2.
%
%
%
%
Record of revisions:
Date
Programmer
====
==========
10/18/98
S. J. Chapman
Description of change
=====================
Original code
Fall 2004
CS 111
15