matlab 字串處理
阿新 • • 發佈:2019-01-07
0. 字串比較
- strcmp()
- strcmpi():大小寫不敏感,case insensitive;
1. deblank
Remove trailing whitespace from end of character array. (刪除序列尾部(注意僅僅是尾部,不包括頭部的空格)的空格)。
2. 字串切割(split)
使用正則表示式:
>> str = 'hello world hello China';
>> splited = regexp(str, ' ', 'split');
>> splited
splited =
'hello' 'world' 'hello' 'China'
注意 regexp(str, ' ', 'split')
得到的是元組型別。
3. strfind():返回元素的下標
>> strfind('hello|world', '|')
6
4. char ⇔ ASCII
(1)ASCII
⇒ char>> char([97, 98, 99]) ans = abc
(2)char ⇒ ASCII
>> abs('abc') ans = 97 98 99
5. 字串的拼接
字串(str1、str2)的拼接使用 [str1, str2] 或 [str1 str2]。
注意,str1 + str2 所做的動作就不是拼接了,而是首先轉換為 ascii 碼型別,再進行相加的操作,這就要求兩串的長度必須相等,
>> str1 = 'hello'; str2 = 'world';
>> str1 + str2
ans =
223 212 222 216 211
>> abs(str1) + abs(str2)
ans =
223 212 222 216 211