字符串 string
1 字符串连接的三种方法
(1)用一种类似于矩阵表达式
>> str1 = 'cxs';
str2 = 'i love you';
strdisp = [str1,',',' ',str2, '!']
str2 = 'i love you';
strdisp = [str1,',',' ',str2, '!']
strdisp =
cxs, i love you!
(2)strcat 函数
下面是两种用向量和矩阵的不同表达式
>> str1 = 'cxs';
str2 = 'i love you';
strcat(str1,str2)
strcat = {str1,str2}
str2 = 'i love you';
strcat(str1,str2)
strcat = {str1,str2}
ans =
cxsi love you
strcat =
'cxs' 'i love you'
但是如果变量没有被提前赋值,那么,用这个函数的时候就不能这么写了
注意 {} [] 的使用的区别
并且要先建立cell
>> a = cell (2,3);
a = {'Red','Yellow','Green','Blue'}
a = {'Red','Yellow','Green','Blue'}
a =
'Red' 'Yellow' 'Green' 'Blue'
>> strcat(['Red','Yellow'],['Green','Blue'])
ans =
RedYellowGreenBlue
>> strcat({'Red','Yellow'},{'Green','Blue'})
ans =
'RedGreen' 'YellowBlue'
(3)sprintf 函数
中间或者最后加上的东西,都要用转义符号
>> a = 'cxs';
b = 'feng';
sprintf('%s loves %s',a,b)
b = 'feng';
sprintf('%s loves %s',a,b)
ans =
cxs loves feng
2 比较字符串
用 strcmp这个命令
如果 a 和 b 两个被赋值的string,判断这个两个string是否一样
那么 strcmp (a,b)
ans = 0 则表示不一致,如果 ans = 1则表示一致
strcmp Compare strings.
TF = strcmp(S1,S2) compares the strings S1 and S2 and returns logical 1
(true) if they are identical, and returns logical 0 (false) otherwise.
TF = strcmp(S1,S2) compares the strings S1 and S2 and returns logical 1
(true) if they are identical, and returns logical 0 (false) otherwise.