當(dāng)前位置:首頁 > IT技術(shù) > 編程語言 > 正文

MatLab---刪除字符+比較字符數(shù)組和字符串
2022-04-25 23:14:34

加號(hào)用于連接字符串

"hello"+"goodbey"
ans =
"hellogoodbey"

方括號(hào)也用于連接字符
>> ['hello','goodbey']
ans =
'hellogoodbey'

strcat() 用于捏合字符,字符串,但是char類型空格不加入,string類型空格會(huì)加入
>> strcat('hello',' ','goodbey')
ans =
'hellogoodbey'
>> strcat("hello","? ?","goodbey")
ans =
"hello? goodbey"
>> strcat("hello","-","goodbey")
ans =
"hello-goodbey"
>> strcat('hello','-','goodbey')
ans =
'hello-goodbey'

sprintf()函數(shù) 打印出空格和換行

sprintf('? ?'),返回char類型;sprintf(”? “),返回string類型;

fprintf('%7.3f ',pi)
3.142
>> sent1=sprintf('%7.3f ',pi)
sent1 =
' 3.142
'

deblank():刪除字符或者字符串結(jié)尾的空格

test_char=' hello '
test_char =
' hello '
>> deblank(test_char)
ans =
' hello'
>> test_string=string(test_char)
test_string =
" hello "
>> deblank(test_string)
ans =
" hello"

strtrim()刪除掉字符和字符串開始和結(jié)尾處的空格;

strtrim(test_char)
ans =
'hello'
>> strtrim(test_string)
ans =
"hello"

strip(test_char_x,'-')

test_char_x='------hello-----'
test_char_x =
'------hello-----'
>> test_string_x=string(test_char_x)
test_string_x =
"------hello-----"
>> strip(test_char_x,'-')
ans =
'hello'
>> strip() 刪除字符或者字符串中的某個(gè)元素
ans =
"hello"

erase() 刪除字符或者字符串中的某個(gè)元素

test_char_x='xxxAxxBxxCxDxExFxxx'
test_char_x =
'xxxAxxBxxCxDxExFxxx'
>> test_string_x=string(test_char_x)
test_string_x =
"xxxAxxBxxCxDxExFxxx"
>> erase(test_char_x,'x')
ans =
'ABCDEF'

lower()? ??upper()字符或者字符串的大小寫轉(zhuǎn)換

lower('AHBGhids')
ans =
'ahbghids'
>> upper('GDcsid')
ans =
'GDCSID'
>> upper("GDcsid")
ans =
"GDCSID"
>> lower("AHBGhids")
ans =
"ahbghids"

字符,字符串的比較

word1='cat'
word1 =
'cat'
>> word2='car'
word2 =
'car'

word3='cathedral'
word3 =
'cathedral'
>> word4='CAR'
word4 =
'CAR'

字符數(shù)組的比較
>> word1==word2
ans =
1×3 logical 數(shù)組
1 1 0
>> word1==word3
矩陣維度必須一致。

strcmp(word1,word2):比較兩者是否相同
>> strcmp(word1,word2)
ans =
logical
0
>> strcmp(word1,word3)
ans =
logical
0
>> strcmp(word1,word1)
ans =
logical
1

strncmp(word1,word3,3)%只比較字符串的前n位的數(shù)組
ans =
logical
1
>> strcmp(upper(word2),upper(word4))
ans =
logical
1
>> strcmpi(word2,word4)%對(duì)于大小寫不敏感
ans =
logical
1

字符串的比較
>> "APPLE"=="apple"
ans =
logical
0
>> strcmp("APPLE","apple")
ans =
logical
0
>> "APPLE"=="appl"
ans =
logical
0

本文摘自 :https://www.cnblogs.com/

開通會(huì)員,享受整站包年服務(wù)立即開通 >