• 當前位置:首頁 > 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/

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码