当前位置:首页 > IT技术 > 编程语言 > 正文

MatLab---删除字符+比较字符数组和字符串
2022-04-25 23:14:34

加号用于连接字符串

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

方括号也用于连接字符
>> ['hello','goodbey']
ans =
'hellogoodbey'

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

sprintf()函数 打印出空格和换行

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

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

deblank():删除字符或者字符串结尾的空格

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()删除掉字符和字符串开始和结尾处的空格;

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() 删除字符或者字符串中的某个元素
ans =
"hello"

erase() 删除字符或者字符串中的某个元素

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()字符或者字符串的大小写转换

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'

字符数组的比较
>> word1==word2
ans =
1×3 logical 数组
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位的数组
ans =
logical
1
>> strcmp(upper(word2),upper(word4))
ans =
logical
1
>> strcmpi(word2,word4)%对于大小写不敏感
ans =
logical
1

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

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

开通会员,享受整站包年服务立即开通 >