Matlab学习笔记五:字符串操作

94.把数值数组转换成字符数组

>> x = [77 65 84 76 65 66];
xx=char(x)

xx =

MATLAB

>> y=int2str(rand(3))

y =

1 0 0
0 1 0
1 1 1


z=mat2str([1 2 3.5])

z =

[1 2 3.5]


a=dec2bin(356)

a =

101100100


b=dec2base(356,7)

b =

1016


whos

  Name      Size                    Bytes  Class

  a         1x9                        18  char array
  b         1x4                         8  char array
  x         1x6                        48  double array
  xx        1x6                        12  char array
  y         3x7                        42  char array
  z         1x9                        18  char array

Grand total is 55 elements using 146 bytes

>> 7^3+7+6

ans =

   356

95.把字符数组转换成数值数组
name = 'Thomas R. Lee';
name = double(name)

name =

84 104 111 109 97 115 32 82 46 32 76 101 101


name = char(name)

name =

Thomas R. Lee


str = '37.294e-1';
val = str2num(str)

val =

3.7294


c = {'37.294e-1'; '-58.375'; '13.796'};
d = str2double(c)

d =

3.7294
-58.3750
13.7960


whos c d

Name Size Bytes Class

c 3x1 224 cell array
d 3x1 24 double array

Grand total is 28 elements using 248 bytes


hex2dec('3ff')

ans =

1023


bin2dec('010111')

ans =

23


base2dec('212',3)   %???????

ans =

    23

96.创建二维字符数组
a='a';
b='bb';
c=''; %创建空字符串
[a;b] %不同长度的字符串不能用方括号纵向连接

??? Error using ==> vertcat
All rows in the bracketed expression must have the same
number of columns.


[a,' ';b] %把第一行字符串用空格扩展成和第二行等长

ans =

a
bb


sAB=strvcat(a,b) %strvcat连接,不要求每行等长
size(sAB)

sAB =

a
bb


ans =

     2     2


sABC=strvcat(a,b,c) %strvcat连接,空字符串被忽略
size(sABC)

sABC =

a
bb


ans =

     2     2


cABC=char(a,b,c) %char连接,空字符串也被空格填满
size(cABC)

cABC =

a
bb
 


ans =

     3     2

97.空格处理函数
a=blanks(4)

a =
length(a)

ans =

     4


b='test    ';
length(b)
ans =

     8


c=deblank(b)

c =

test


length(c)
ans =

     4


d='   test    ';
length(d)

ans =

11


e=strtrim(d)

e =

test


length(e)
ans =

     4

98.两个字符串逐个字符的比较
a='aa';
aa='aa  ';
b='abcd';
c='c';
a==b %不等长的字符串不能用==比较

??? Error using ==> eq
Array dimensions must match for binary array op.
aa==b

ans =

     1     0     0     0


c==b %单个字符和字符串之间可以用==比较


ans =

     0     0     1     0

99.正则表达式简单应用
stra = 'bat cat can car COAT court cut ct CAT-scan';
regexpi(stra, 'c[aeiou]+t') %匹配c*t模式的字符串,其中*是aeiou中的某一个字符

ans =

5 17 28 35


strb = {'Madrid, Spain' 'Romeo and Juliet' 'MATLAB is great'};
s1 = regexp(strb, '[A-Z]'); %匹配大写字母
s2 = regexp(strb, '\s');  %匹配空格字符
celldisp(s1)

s1{1} =

1 9 
 
s1{2} =
 
     1    11

 
 
s1{3} =
 
     1     2     3     4     5     6

 
celldisp(s2) 
s2{1} =
 
     8

 
 
s2{2} =
 
     6    10

 
 
s2{3} =
 
     7    10

100.字符串的比较
a='abcde';
b='abcgh';
c='abcDE';
d='aBCde';
strcmp(a,b)

ans =

0


strncmp(a,b,3)

ans =

1


strcmp(a,c)  %strcmp比较,有大小写差别

ans =

0


strcmpi(a,c) %ctrcmpi比较时,忽略大小写差别

ans =

1


strncmp(b,d,3)

ans =

0


strncmpi(b,d,3)
ans =

     1

101.字符串的查找
a='Welcome to MATLAB!';
b='Welcome,Sir!';
c='Welcome';
ab=strcat(a,b)
strfind(ab,c) %strfind中长字符串在前,否则返回空数组
strfind(c,ab)
findstr(ab,c) %findstr中两个字符串位置可变
findstr(c,ab)
strmatch(a,c) %strmatch中长字符串应在后
strmatch(c,a)
strmatch(c,a,'exact')
[wel1,rem1]=strtok(ab,'!')
[wel2,rem2]=strtok(rem1,'!')

 

ab =

Welcome to MATLAB!Welcome,Sir!


ans =

     1    19


ans =

     []


ans =

     1    19


ans =

     1    19


ans =

     []


ans =

     1


ans =

     []


wel1 =

Welcome to MATLAB


rem1 =

!Welcome,Sir!


wel2 =

Welcome,Sir


rem2 =

!

 

102.字符串的创建
a='   a';b='b  b';c='c   ';  %用单引号创建字符串,都由四个字符组成
length(a),length(b),length(c) %获取字符串长度
ABC=[a b c]  %用方括号连接三个字符串,空格不被裁切
length(ABC)
sAC=strcat(a,c) %用strcat函数连接字符串,每个字符串最右边的空格被裁切
length(sAC)
AC=[a,c]
length(AC)
whos  %显示工作区内所有变量,每一个字符占用两个字节

ans =

     4


ans =

     4


ans =

     4


ABC =

   ab  bc  


ans =

    12


sAC =

   ac


ans =

     5


AC =

   ac  


ans =

     8

  Name      Size                    Bytes  Class

  ABC       1x12                       24  char array
  AC        1x8                        16  char array
  a         1x4                         8  char array
  ans       1x1                         8  double array
  b         1x4                         8  char array
  c         1x4                         8  char array
  sAC       1x5                        10  char array

Grand total is 38 elements using 82 bytes

 

103.字符串的替换
str='Error Msg';
newstr=strrep(str,'Msg','Mail')
newstrr=strrep(str,'msg','Mail') %strrep对大小写敏感,替换没有进行


newstr =

Error Mail


newstrr =

Error Msg

 

104.字符串元胞数组的操作
a={'   a','bbbb';'c   ',' dd '}
b={'tt  ','  tt';'  bb','bb  '}
strcat(a,b)
strncmp(a,b,2)
c=strcat(a,b)
strrep(c,'tt','TT')
deblank(c)
strtrim(c)
upper(c)
strjust(c,'left') '
iscellstr(c)


a =

    '   a'    'bbbb'
    'c   '    ' dd '


b =

    'tt  '    '  tt'
    '  bb'    'bb  '


ans =

    '   att  '    'bbbb  tt'
    'c     bb'    ' dd bb  '


ans =

     0     0
     0     0


c =

    '   att  '    'bbbb  tt'
    'c     bb'    ' dd bb  '


ans =

    '   aTT  '    'bbbb  TT'
    'c     bb'    ' dd bb  '


ans =

    '   att'      'bbbb  tt'
    'c     bb'    ' dd bb' 


ans =

    'att'         'bbbb  tt'
    'c     bb'    'dd bb'  


ans =

    '   ATT  '    'BBBB  TT'
    'C     BB'    ' DD BB  '

??? strjust(c,'left') '
                      |
Error: Missing variable or function.

 

105.字符归属测试函数
a='Building 17#';
isletter(a)
isspace(a)

ans =

     1     1     1     1     1     1     1     1     0     0     0     0


ans =

     0     0     0     0     0     0     0     0     1     0     0     0

 

106.字符数组的格式操作
a=strvcat('Welcome','to','MathWorks')
upper(a)
strjust(a,'center')
sort(a(3,:)) %对字符数组a的第三行字符串,按照升序排列
sort(a) %对字符数组a的每一列,按照字符升序排列
sort(a,2) %对字符数组a的每一行,按照字符升序排列
sort(a,2,'descend') %对字符数组a的每一行,按照字符降序排列

a =

Welcome 
to      
MathWorks


ans =

WELCOME 
TO      
MATHWORKS


ans =

 Welcome
   to   
MathWorks


ans =

MWahkorst


ans =

Ma      
WelcWme 
tothoorks


ans =

  Wceelmo
       ot
MWahkorst


ans =

omleecW 
to      
tsrokhaWM

 

107.字符数组和字符串的元胞数组之间的转换
data = ['Allison Jones';'Development  ';'Phoenix      '];
celldata = cellstr(data)
length(celldata{3})
iscellstr(celldata) %测试变量是否属于字符串的元胞数组
strings = char(celldata)
length(strings(3,:))

celldata =

    'Allison Jones'
    'Development'
    'Phoenix'


ans =

     7


ans =

     1


strings =

Allison Jones
Development 
Phoenix     


ans =

    13

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值