MatLab---字符数组和字符串的搜索,替代,分割和合并,文本信息类型判别和检测

1.strcmp()与isequal()

strcmp({'A','B'},{'A','B'}) %数组中的每个元素进行判断

ans =

1×2 logical 数组

1 1

>> isequal({'A','B'},{'A','B'}) %数组整体进行判断
ans =
logical
1
>> strcmp('Apple','Apple')
ans =
logical
1
>> isequal('Apple','Apple')
ans =
logical
1
>> strcmp("apple","Apple")
ans =
logical
0
>> isequal("apple","Apple")
ans =
logical
0

2.字符串的查找与替换

strfind():查找  strrep():替换  count() :子字符串出现的次数

strfind('hiabcd','a')
ans =
3
>> strfind("abcdef","bc")
ans =
2
>> strfind('abddddhujmd','d')
ans =
3 4 5 6 11
>> strfind('hdish','yyy')
ans =
[]
>> strrep('hidsvcdcccdddss','d','x')
ans =
'hixsvcxcccxxxss'
>> strrep('abchishyuhabc','---')
错误使用 strrep
输入参数的数目不足。
>> strrep('abchishyuhabc','abc','---')
ans =
'---hishyuh---'
>> count('abcjijgirabc','abc')
ans =
2
>> count('abcfs','')
ans =
6
>> count("abcfs",'')
ans =
6
>> %在字符串或者字符中,计算空字符串的个数,
>> %每个字符之间有,字符串的前后也有
>>
>> %每个字符之间有,字符串的前后也有

3.字符串的分割

strok()

[word,rest]=strtok("hello there","")  %以空值分割
word =
"hello there"
rest =
""
>> [word,rest]=strtok("hello there","  ")  %空格分割,空格放入下一个字符串中
word =
"hello"
rest =
" there"
>> [word,rest]=strtok("hello there","t")  %以t分割,t放在下一个字符串中
word =
"hello "
rest =
"there"
>> [word,rest]=strtok("hello there")  %自动以空格分割
word =
"hello"
rest =
" there"
>> [word,rest]=strtok("hello there","tl")  %以t或者l分割
word =
"he"
rest =
"llo there"
>> [word,rest]=strtok("hello there","x")  %以字符串中没有的字符x进行分割
word =
"hello there"
rest =
""

4.对string变量的操作

 

strings(2,4)
ans =
2×4 string 数组
"" "" "" ""
"" "" "" ""
>> majors=["English","History","Engineering"]
majors =
1×3 string 数组
"English" "History" "Engineering"
>> strlength(majors)  %数组中各个元素的长度
ans =
7 7 11
>> upper(majors)
ans =
1×3 string 数组
"ENGLISH" "HISTORY" "ENGINEERING"
>> "BA degree in"+majors   字符串标量与数组的捏合
ans =
1×3 string 数组
列 1 至 2
"BA degree inEnglish" "BA degree inHistory"
列 3
"BA degree inEngin…"
>> degreees=["BA","BA","BS"]
degreees =
1×3 string 数组
"BA" "BA" "BS"
 %数组与数组之间的捏合
>> degreees+"in"+majors
ans =
1×3 string 数组
列 1 至 2
"BAinEnglish" "BAinHistory"
列 3
"BSinEngineering"
>> degreees+" in "+majors
ans =
1×3 string 数组
列 1 至 2
"BA in English" "BA in History"
列 3
"BS in Engineering"
>> strjoin(majors)  %将所有捏合在一起
ans =
"English History Engineering"
>> [degreees;majors]  %数组组合在一起
ans =
2×3 string 数组
"BA" "BA" "BS"
"English" "History" "Engineering"
>> [degreees;majors]'  %转置
ans =
3×2 string 数组
"BA" "English"
"BA" "History"
"BS" "Engineering"
>> news=[degreees;majors]'
news =
3×2 string 数组
"BA" "English"
"BA" "History"
"BS" "Engineering"
>> join(news)  %每行之间进行捏合
ans =
3×1 string 数组
"BA English"
"BA History"
"BS Engineering"
>> strjoin(news)
ans =
"BA BA BS English History Engineering"
>> %join()是将每行捏合在一起;而strjoin()是将所有的捏合在一起

5.类型判断与检测

isletter('a') %判断是否是字母
ans =
logical
1
>> isletter('1')
ans =
logical
0
>> isletter("A")
ans =
logical
1
>> isletter("6")
ans =
logical
0
>> isletter("ji545dcd")   
ans =
1×8 logical 数组
1 1 0 0 0 1 1 1
>> isletter('hids564dsdf5')
ans =
1×12 logical 数组
1 1 1 1 0 0 0 1 1 1 1 0
>> isspace('hello world')  %判断空格
ans =
1×11 logical 数组
0 0 0 0 0 1 0 0 0 0 0
>> ischar('chd') %判断是否是char类型
ans =
logical
1
>> isstring('apple')
ans =
logical
0
>> isstring("apple") 判断是否是string类型
ans =
logical
1
>> isstring(["apple","banana"])
ans =
logical
1
>> isStringScalar(["apple","banana"])  %判断是否是string类型的标量
ans =
logical
0
>> isStringScalar("apple")
ans =
logical
1
>> contains("hello","0")  字符串中是否包含某个元素
ans =
logical
0
>> contains("hello","o")
ans =
logical
1
>> majors
majors =
1×3 string 数组
"English" "History" "Engineering"
>> contains(majors,'Eng')  %数组中的每个元素,是否包含某个元素
ans =
1×3 logical 数组
1 0 1
>> contains(majors,"Eng")
ans =
1×3 logical 数组
1 0 1
>> startsWith(majors,"Eng")  字符串的开头是否包含某个元素
ans =
1×3 logical 数组
1 0 1
>> endsWith(majors,"sh")  字符串的结尾是否包含某个元素
ans = 
1×3 logical 数组
1 0 0
>> contains("apple","") 判断是否包含空字符,在每个元素之间和开头结尾处,都有空字符
ans =
logical
1
>> endsWith("apple","")
ans =
logical
1
>> startsWith("apple","")
ans =
logical
1
>> strfind("apple","") %虽然有空字符,但是标记不出具体的位置
ans =
[]
>> strfind("Apple","A")
ans =
1
>> %空字符串无法标记它的位置

posted @ 2022-04-26 10:58  无敌小金刚  阅读(1418)  评论(0)    收藏  举报