【MATLAB|基础】字符串操作

本文介绍了MATLAB中一系列与字符串操作相关的函数,如char用于数值转字符串,double反之,以及blanks、deblank、ischar、strcat、strvcat、strcmp、findstr等函数,展示了字符串处理、类型转换和比较的常用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1.char 函数将数值转为字符串


>> a=120

a =

   120

>> class(a)

ans =

    'double'

>> size(a)

ans =

     1     1

>> a=char('Hellow','World')

a =

  2×6 char 数组

    'Hellow'
    'World '

>> class(a)

ans =

    'char'

>> size(a)

ans =

     2     6

2.double 函数将字符串转为数值

>> a=double(a)

a =

    72   101   108   108   111   119
    87   111   114   108   100    32

>> class(a)

ans =

    'double'

>> size(a)

ans =

     2     6

3.blanks 函数创建空字符串

>> a=blanks(8)

a =

    '        '

4.deblank 函数去掉字符串末尾空格

>> a='abc   '

a =

    'abc   '

>> deblank(a)

ans =

    'abc'

5.ischar 函数判断变量是否为字符类型

>> a='1'

a =

    '1'

>> ischar(a)

ans =

  logical

   1

>> b=1

b =

     1

>> ischar(b)

ans =

  logical

   0

6.strcat 函数水平组合字符串 

>> a='hello'

a =

    'hello'

>> b='world'

b =

    'world'

>> c=strcat(a,b)

c =

    'helloworld'

7.strvcat 函数创建字符矩阵

>> a='hello'

a =

    'hello'

>> b='world'

b =

    'world'

>> d=strvcat(a,b)

d =

  2×5 char 数组

    'hello'
    'world'

8.strcmp 函数比较字符串 

>> a='abc'

a =

    'abc'

>> b='abc'

b =

    'abc'

>> strcmp(a,b)

ans =

  logical

   1

9.strncmp 函数比较字符串前n个字符

>> a='abc'

a =

    'abc'

>> b='abcd'

b =

    'abcd'

>> strncmp(a,b,3)

ans =

  logical

   1

10.findstr 函数在长字符串中查找短字符串索引

>> a='abcd'

a =

    'abcd'

>> b='bc'

b =

    'bc'

>> c=findstr(a,b)

c =

     2

11.strfind 函数在第一个字符串中查找第二个字符串的索引

>> a='abcd'

a =

    'abcd'

>> b='bc'

b =

    'bc'

>> c=findstr(a,b)

c =

     2

>> d=findstr(b,a)

d =

     2

12.strjust 函数对齐排列字符串

>> a='hello'

a =

    'hello'

>> b='world'

b =

    'world'

>> c=strvcat(a,b)

c =

  2×5 char 数组

    'hello'
    'world'

>> d=strjust(c)

d =

  2×5 char 数组

    'hello'
    'world'

 13.strrep 函数替换字符串中的字串

>> s1='hallo world'

s1 =

    'hallo world'

>> s2=strrep(s1,'hallow','hello')

s2 =

    'hallo world'

 14.strmatch 函数查询匹配的字符串

>> s=strmatch('ab',strvcat('abandon','absorb','about'))

s =

     1
     2
     3

15.strcmpi 函数查询字符串是否匹配(忽略大小写)

>> a=strcmpi('abc','ABC')

a =

  logical

   1

 16.strncmpi 函数查询字符串前n个字符是否匹配(忽略大小写)

>> a=strncmpi('abcde','AbCef',3)

a =

  logical

   1

 17.upper 函数将字符串转为全部大写

>> s1='Hello World'

s1 =

    'Hello World'

>> s2=upper(s1)

s2 =

    'HELLO WORLD'

18.lower 函数将字符串转为全部小写

>> s1='Hello World'

s1 =

    'Hello World'

>> s3=lower(s1)

s3 =

    'hello world'
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值