一 字符串分割
matlab中最常用的字符串分割函数有两个,都比较好用,分别是strsplit和strtok。
1 strsplit
假设需要分割的字符串为str,直接使用 strsplit(str) 就可以分割,默认按空白字符分割,分割后的字符组成元胞数组。
>> str = ‘hello world, I am a student!‘
str =
hello world, I am a student!
>> s = strsplit(str);
>> s
s =
1×6 cell 数组
‘hello‘ ‘world,‘ ‘I‘ ‘am‘ ‘a‘ ‘student!‘
>> s{1,1}
ans =
hello
>> s{1,2}
ans =
world,
>>
strsplit的第二个参数可以是分割字符,比如用‘,‘或者‘.‘或者‘-‘等进行字符串的分割,第二个参数甚至可以是包含多个分割字符的元胞数组,如下
>> str = ‘With,the,development,of,society.people-have-higher-requirements-for-image-quality‘
str =
With,the,development,of,society.people-have-higher-requirements-for-image-quality
>> s1 = strsplit(str,‘,‘)
s1 =
1×5 cell 数组
‘With‘ ‘the‘ ‘development‘ ‘of‘ ‘society.people-have-…‘
>>