Documentation

Create Character Arrays

创建字符向量

Create acharacter vector通过将字符序列封闭在单引号标记中。

chr ='你好世界'
chr = 'Hello, world'

Character vectors are1-经过-narrays of typechar。In computer programming,string是一个经常使用的术语1-经过-n字符数组。但是,从R2016B MATLAB开始®also provides astring数据类型,所以1-经过-n字符阵列称为MATLAB文档中的字符向量。

whoschr
名称大小字节类属性chr 1x12 24 char

If the text contains a single quotation mark, include two quotation marks when assigning the character vector.

newChr ='You''re right'
newchr ='你是对的'

功能,例如uint16convert characters to their numeric codes.

chrNumeric = uint16(chr)
chrNumeric = 1×12 uint16 row vector 72 101 108 108 111 44 32 119 111 114 108 100

Thechar函数将整数向量转换回字符。

chrAlpha = char([72 101 108 108 111 44 32 119 111 114 108 100])
chrAlpha = 'Hello, world'

Create Rectangular Character Array

字符阵列是m-经过-narrays of characters, wheremis not always1。You can join two or more character vectors together to create a character array. This is calledconcatenationand is explained for numeric arrays in the sectionConcatenating Matrices。As with numeric arrays, you can combine character arrays vertically or horizontally to create a new character array.

However, it is recommended that you store character vectors in a单元阵列,而不是使用m-经过-ncharacter arrays. Cell arrays are flexible containers that allow you to easily store character vectors of varying length.

Combine Character Vectors Vertically

To combine character vectors into a two-dimensional character array, use square brackets or thechar功能。

  • Apply the MATLAB concatenation operator,[]。Separate each row with a semicolon (;). Each row must contain the same number of characters. For example, combine three character vectors of equal length:

    devTitle = ['Thomas R. Lee';。。。'Sr. Developer';。。。“ Sftware Corp.”]
    devTitle = 3×13 char array 'Thomas R. Lee' 'Sr. Developer' 'SFTware Corp.'

    如果字符向量的长度不同,pad with space characters as needed. For example:

    mgrTitle = ['Harold A. Jorgensen';。。。“助理项目经理”;。。。'SFTware Corp. ']
    mgrTitle = 3×25 char array 'Harold A. Jorgensen ' 'Assistant Project Manager' 'SFTware Corp. '
  • Call thechar功能。如果字符向量的长度不同,charpads the shorter vectors with trailing blanks so that each row has the same number of characters.

    mgrTitle = char('Harold A. Jorgensen',。。。“助理项目经理”,“ Sftware Corp.”)
    mgrTitle = 3×25 char array 'Harold A. Jorgensen ' 'Assistant Project Manager' 'SFTware Corp. '

水平结合角色向量

To combine character vectors into a single row vector, use square brackets or thestrcat功能。

  • Apply the MATLAB concatenation operator,[]。Separate the input character vectors with a comma or a space. This method preserves any trailing spaces in the input arrays.

    name ='Thomas R. Lee';标题='Sr. Developer';company =“ Sftware Corp.”;fullName = [name','title','公司]

    MATLAB returns

    fullName = 'Thomas R. Lee, Sr. Developer, SFTware Corp.'
  • Call the concatenation function,strcat。此方法可以删除输入中的尾随空间。例如,组合角色向量以创建假设的电子邮件地址。

    name ='myname ';domain ='mydomain ';ext ='com';address = strcat(name,'@', domain,'.', ext)

    MATLAB returns

    address = 'myname@mydomain.com'

识别字符

使用以下任何功能识别字符数组或字符数组中的某些字符。

功能 Description
ischar Determine whether the input is a character array
isletter 在输入字符阵列中查找所有字母字母
isspace Find all space characters in the input character array
isstrprop 查找特定类别的所有字符

发现一个字符的空间向量。

chr =“在此字符向量中找到空间字符”;% | | | | | | |% 5 9 15 26 29 34 44查找(ISSPACE(CHR))
ANS = 5 9 15 26 29 34 44

Work with Space Characters

Theblanks函数创建一个空间字符的字符向量。创建15个空间字符的向量。字符矢量总是在单引号之间显示。

chr = blanks(15)
chr = ' '

Insert a few nonspace characters in the middle of the blank character vector.

chr(6:10)='AAAAA'
chr = ' AAAAA '

You can justify the positioning of these characters to the left or right using thestrjustfunction:

chrleft = strjust(chr,'剩下')
chrLeft = 'AAAAA '
chrRight = strjust(chr,'正确的')
chrRight = ' AAAAA'

Remove all trailing space characters withdeblank:

chrdeblank = deBlank(chr)
chrdeblank ='aaaaa'

Remove all leading and trailing spaces withstrtrim:

chrTrim = strtrim(chr)
chrtrim ='aaaaa'

Expand Character Arrays

Generally, MathWorks®does not recommend expanding the size of an existing character array by assigning additional characters to indices beyond the bounds of the array such that part of the array becomes padded with zeros.

这个话题有帮助吗?