Chapter 4 - Part 3 - Matlab - Strings
Chapter 4 - Part 3 - Matlab - Strings
a 97 n 110 A 65 N 78
b 98 o 111 B 66 O 79
c 99 p 112 C 67 P 80
d 100 q 113 D 68 Q 81
e 101 r 114 E 69 R 82
f 102 s 115 F 70 S 83
g 103 t 116 G 71 T 84
h 104 u 117 H 72 U 85
i 105 v 118 I 73 V 86
j 106 w 119 J 74 W 87
k 107 x 120 K 75 X 88
l 108 y 121 L 76 Y 89
m 109 z 122 M 77 Z 90
Symbol Digit Symbol Digit Symbol Digit Symbol Digit
32 - 45 : 58 { 123
! 33 X. 46 ; 59 | 124
“ 34 / 47 < 60 } 125
# 35 0 48 = 61
$ 36 1 49 > 62
% 37 2 50 ? 63
& 38 3 51 @ 64
‘ 39 4 52 [ 91
( 40 5 53 \ 92
) 41 6 54 ] 93
* 42 7 55 ^ 94
+ 43 8 56 _ 95
, 44 9 57 ` 96
Conversion of strings to numerical arrays
◦ double( 'abc xyz' )
ans =
97 98 99 32 120 121 122
◦ double( 'ABC XYZ' )
ans =
65 66 67 32 88 89 90
Conversion of numerical arrays to strings
◦ char( [ 72 101 108 108 111 33 ] )
ans =
Hello!
ischar() : returns 1 for a character array
◦ ischar ( 'CS 111' )
ans =
1
isletter() : returns 1 for letters of the alphabet
◦ isletter( 'CS 111' )
ans =
1 1 0 0 0 0
isspace() : returns 1 for whitespace (blank, tab, new
line)
◦ isspace( 'CS 111' )
ans =
0 0 1 0 0 0
Comparing two characters
◦ 'a' < 'e'
ans =
1
Comparing two strings character by character
◦ 'fate' == 'cake'
ans =
0 1 0 1
◦ 'fate' > 'cake'
ans =
1 0 1 0
strcmp() : returns 1 if two strings are identical
◦ a = 'Bilkent';
◦ strcmp( a, 'Bilkent' )
ans =
1
◦ strcmp( 'Hello', 'hello' )
ans =
0
strcmpi() : returns 1 if two strings are identical
ignoring case
◦ strcmpi( 'Hello', 'hello' )
ans =
1
findstr() : finds one string within another one
◦ test = 'This is a test!';
◦ pos = findstr( test, 'is' )
pos =
3 6
◦ pos = findstr( test, ' ' )
pos =
5 8 10
strrep() : replaces one string with another
◦ s1 = 'This is a good example';
◦ s2 = strrep( s1, 'good', 'great' )
s2 =
This is a great example
Recall num2str() for numeric-to-string
conversion
◦ str = [ 'Plot for x = ' num2str( 10.3 ) ]
str =
Plot for x = 10.3
string1 =
abcdef
>> string2='abcdef'
string2 =
abcdef
ans =
0
Solution