29. 字符数组和字符串数组、文本操作常用函数
29.1 字符数组和字符串数组
Apple_char = 'Apple' ; % 向量形式存储
class ( Apple_char) ; % char
Apple_string = "Apple" ; % 标量形式存储
class ( Apple_string) ; % string
double( Apple_char) ; % 得到字符对应数字,65 112 112 108 101
size( Apple_char) ; % 1 5
length( Apple_char) ; % 5 ,返回行或列数最大值
Apple_char( 2 ) ; % 'p'
size( Apple_string) ; % 1 1
length( Apple_string) ; % 1
Apple_string( 1 ) ; % "Apple"
Apple_string{ 1 } ; % 'Apple' , 将字符串变为字符
Apple_string{ 1 } ( 2 ) ; % 'p'
majors = [ "English" , "History" , "Engineering" ] ; % majors = "English" "History" "Engineering"
majors_char = [ 'English' , 'History' , 'Engineering' ] ; % majors_char = 'EnglishHistoryEngineering'
majors_char = char( 'English' , 'History' , 'Engineering' ) ;
% 3 ×11 char 数组
% 'English '
% 'History '
% 'Engineering'
'' ; % 0 行0 列
[ '' , '' ] ; % 0 行0 列
[ "" , "" ] ; % 1 行2 列
29.2 文本操作常用函数
input ( 'Enter some:' , 's' ) ; % 输入字符为字符类型
repmat( 'a' , 4 , 3 ) ;
% 4 ×3 char 数组
% 'aaa'
% 'aaa'
% 'aaa'
% 'aaa'
[ 'a' blanks( 5 ) 'b' ] ; % 'a b'
string( 'Apple' ) ; % "Apple"
string( [ 65 112 112 108 101 ] ) ; % 1 ×5 string 数组, "65" "112" "112" "108" "101"
char( [ 65 112 112 108 101 ] ) ; % 'Apple'
string( char( [ 65 112 112 108 101 ] ) ) ; % "Apple"
"a" + "b" ; % "ab"
[ 'a' 'b' ] ; % 'a b'
strcat( 'a' , 'b' , ' ' , 'c' ) ; % 'abc'
strcat( "a" , "b" , " " , "c" ) ; % "ab c"
fprintf( '%7.3f \n' , pi) ; % 3.142
sent1 = sprintf( '%7.3f \n' , pi) ; % 用于将输出内容转发给其它变量
% ' 3.142
% '
a = 1 ;
title( sprintf( 'title is %d' , a) ) ; % 动态改变标题名
username = input ( 'Please enter your name:' , 's' ) ;
id_no = input ( sprintf( '%s,enter your id #:' , username) ) ; % 用sprintf( ) 转为字符,传入input 函数
30. 删除字符,比较字符数组与字符串
30.1 删除字符
test_char = ' q a ' ;
deblank( test_char) ; % 删除后边的空格, ' q a'
strtrim( test_char) ; % 删除前后所有空格,'q a'
test_char = '-a-' ;
strip( test_char, '-' ) ; % 第二个参数是要删除的符号,'a'
tets_str = "-a" ;
strip( test_str, '-' ) ; % 第二个参数是要删除的符号,"a"
test_char = 'xxAxBx' ;
erase( test_char, 'x' ) ; % 第二个参数是要删除的字符,可以是多个,'AB'
erase( test_char, 'xA' ) ; % 'B'
tets_str = "xxAxBx" ;
erase( tets_str, 'x' ) ; % 第二个参数是要删除的字符,可以是多个,"AB"
erase( tets_str, 'xA' ) ; % "B"
lower( 'ABC' ) ; % 'abc'
upper( 'abc' ) ; % 'ABC'
30.2 比较字符数组与字符串
'abc' == 'acb' ; % 长度必须相同,logical, 1 0 0
'abc' == 'acbd' ; % 错误
strcmp( 'abc' , 'acb' ) ; % logical, 0
strcmp( 'abc' , 'acbd' ) ; % logical, 0
strcmp( 'abc' , 'abc' ) ; % logical, 1
strncmp( 'abc' , 'abcd' , 3 ) ; % 比较前3 个字符, logical, 1
strcmpi( 'abC' , 'ABc' ) ; % 不区分大小写,logical , 1
isequal( 'abc' , 'abcd' ) ; % logical, 0
"abc" = "acb" ; % logical , 0
strcmp( "abc" , "abcd" ) ; % logical , 0
isequal( "abc" , "abcd" ) ; % logical, 0
[ 'a' , 'b' ] ; % 'ab'
[ "a" , "b" ] ; % 1 ×2 string 数组, "a" "b"
{ 'a' , 'b' } ; % 1 ×2 cell 数组, { 'a' } { 'b' }
{ "a" , "b" } ; % 1 ×2 cell 数组, { [ "a" ] } { [ "b" ] }
strcmp( [ "a" , "b" ] , [ "a" , "c" ] ) ; % 1 ×2 logical 数组, 1 0
isequal( [ "a" , "b" ] , [ "a" , "c" ] ) ; % 0
strcmp( { 'a' , 'b' } , { 'a' , 'c' } ) ; % 1 ×2 logical 数组, 1 0
isequal( { 'a' , 'b' } , { 'a' , 'c' } ) ; % 0
31. 字符数组和字符串的搜索、替换,分割和合并,文本信息的判断与检测
31.1 字符数组和字符串的搜索、替换
strfind( 'abcd' , 'd' ) ; % 4 , 找到第二参数在第一个参数中的位置
strfind( "abcde" , "bc" ) ; % 2
strfind( 'abab' , 'a' ) ; % 1 3
strfind( 'abab' , 'a' , 'c' ) ; % cbcb, 用‘c'替换' a'
31.2 字符数组和字符串的统计
count( 'abab' , 'a' ) ; % 2 , 'a' 字符出现的次数
count( 'abab' , '' ) ; % 6 , 字符之间是一个''
count( "abab" , "" ) ; % 6
31.3 字符数组和字符串的分割
[ word, rest] = strtok( "Hello there" , " " ) ; % word = "Hello" rest = " there"
[ word, rest] = strtok( "Hello there" ) ; % word = "Hello" rest = " there"
[ word, rest] = strtok( "Hello there" , "tl" ) ; % 以t或l进行分割,谁先出现,就按谁分割。word = "He" rest = "llo there"
[ word, rest] = strtok( "Hello there" , "Y" ) ; % word = "Hello there" rest = ""
31.4 字符数组和字符串的合并
strings( 2 , 4 ) ; % 2 行4 列string类型数组
majors = [ "a" , "b" , "c" ] ;
upper( majors) ; % 大写, "A" "B" "C"
"I am " + majors; % 标量+ 向量,1 ×3 string 数组, "I am a" "I am b" "I am c"
degrees = [ "d" , "e" , "f" ] ;
majors + degrees; % 向量+ 向量,1 ×3 string 数组,"ad"
newsa = [ majors; degrees] ;
% 2 ×3 string 数组
% "a" "b" "c"
% "d" "e" "f"
join( newsa) ; % 把每一行合并
% 2 ×1 string 数组
% "a b c"
% "d e f"
strjoin( newsa) ; % 把所有行合并,"a d b e c f"
31.5 文本信息的判断与检测
isletter( 'A' ) ; % 判断字符,1
isletter( 'ab' ) ; % 1 1
isspabe( 'a b' ) ; % 判断空格,0 1 0
ischar( 'abc' ) ; % 判断字符类型, 1
isstring( "abc" ) ; % 判断字符串类型, 1
isstring( [ "ab" , "cb" ] ) ; % 1
isStringScalar( "1" ) ; % 判断是否是标量,1
isStringScalar( [ "1" , "b" ] ) ; % 0
contains( "hello" , "e" ) ; % 是否包含“e", 1
contains( [ "ab" , "ac" , "et" ] , "a" ) ; % 1 1 0
contains( "hello" , "" ) ; % 是否空白字符存在,1
startsWith( [ "ab" , "ac" , "et" ] , "a" ) ; % 是否以"a" 开头, 1 1 0
endsWith( [ "ab" , "ac" , "et" ] , "a" ) ; % 是否以"a" 结尾, 0 0 0
32. 文本类型和数值类型的转换
num = 38 ;
int2str( num) ; % '38'
int2str( pi) ; % '3'
num2str( pi) ; % '3.1456'
int2str( 2 : 5 ) : % '2 3 4 5'
num2str( 2 : 5 ) ; % '2 3 4 5'
num2str( 3.456789 , 3 ) ; % 保留三位数, '3.46'
num2str( 3.456789 , '%06.2f' ) ; % '003.46'
str2double( '1.23' ) ; % 只能识别一个数字,1.23
str2double( '123 456' ) ; % NaN
str2double( [ "123" , "456" ] ) ; % 123 456
str2num( '1.23' ) ; % 1.23
str2num( '123 456' ) ; % 能识别多个数字,123 456
str2num( [ "123" , "456" ] ) ; % 错误,输入必须为字符向量或字符串标量
str2num( [ '123' , '456' ] ) ; % 123456
char( 92 ) ; % '\'
string( 92 ) ; % "92"
char( 54 : 55 ) ; % '67'
string( 54 : 55 ) ; % "54" "55"