matlab|【无标题】Matlab_字符(串)操作(函数)

一个 MATLAB 字符串是一个 char 型数组。每一个字型占两个字节。
示例:

%定义字符串变量 str='hEllo12'; str2='123321.12'; str3='lo' str4='hell' str5='h e l l o '%% %转换为数值型 %将参数y转为双精度浮点类型,如果y是字符,将返回字符的ASCII码值 double_ASCII=double(str) %字符串转换为ASCLL码 abs_ASCII=abs(str) %字符串转换为数值 str2num_double=str2num(str2) eval_double=eval(str2) %根据格式化转义字符转化为相应的数字 sscanf_value=https://www.it610.com/article/sscanf(str2,'%f')%“%f”-浮点,“%g”-整数%% %转换为char型 %ASCLL码转换为字符 char_str=char(double_ASCII) %ASCLL码转换为字符 setstr_str=setstr(double_ASCII) %整数转换为字符串 int2str_str=int2str(double_ASCII) %转换为16进制 dec_2_hex=dec2hex(double_ASCII) %str转换为ASCII码 sprintf_str = sprintf('For str the value of ASCII = %4.2f\n',str) %% %大小写转换 str_upper=upper(str) str_lower=lower(str)%% %查找和比较 %返回短字符串在长字符串中的位置,1为起始 pos=findstr(str3,str) %替换字符串str中str3的内容 str_rep=strrep(str,str3,'-rep-') %比较字符串是否相等,不相等返回0,否则返回1 comp=strcmp(str,str2) comp_i=strcmpi(str,str2) %比较字符串签4个字符是否相等,不相等返回0,否则返回1 comp_n=strncmp(str,str4,4) comp_i_n=strncmpi(str,str4,4) %返回输入字符串中第一次出现在分隔符前面的所有字符,默认的分隔符为一系列的空白字符 %可以利用函数strtok 把一个句子转换为单词 [token, remainder]=strtok(",this,is,my,job!",',') %对字符串内的字符进行逐个判断,判断是否为字母 is_letter=isletter(str) %对字符串内的字符进行逐个判断,判断是否为空白字符(空格,tab,换行符) is_space=isspace(str5) %字符串水平连接,变成一个字符串 str_str2=strcat(str,str2) %竖直地连接两字符串,自动地把它转化为二维数组 str_str2_v=strvcat(str,str2)%% %一个数组中每一行的长度都必须相等,否则会报错比如:name=[str,str3] %创建二维数组可以用 char 函数。函数将会自动地寻找所有字符串中 %最长的那一个成功创建字符串数组 name_array=char(str,str2,str3,str4) %查看二维数组行开头的字符,并返回那些以指定的字符序列为开头行号 str_match=strmatch('hell',name_array) %查看变量属性 whos

运行结果:
>> string_teststr3 ='lo'str4 ='hell'str5 ='h e l l o 'double_ASCII =104691081081114950abs_ASCII =104691081081114950str2num_double =1.2332e+05eval_double =1.2332e+05sscanf_value =https://www.it610.com/article/1.2332e+05char_str ='hEllo12'setstr_str ='hEllo12'int2str_str ='104691081081114950'dec_2_hex =7×2 char 数组'68' '45' '6C' '6C' '6F' '31' '32'sprintf_str ='For str the value of ASCII = 104.00 For str the value of ASCII = 69.00 For str the value of ASCII = 108.00 For str the value of ASCII = 108.00 For str the value of ASCII = 111.00 For str the value of ASCII = 49.00 For str the value of ASCII = 50.00 'str_upper ='HELLO12'str_lower ='hello12'pos =4str_rep ='hEl-rep-12'comp =logical0comp_i =logical0comp_n =logical0comp_i_n =logical1token = "this"remainder = ",is,my,job!"is_letter =1×7 logical 数组1111100is_space =1×10 logical 数组0101010101str_str2 ='hEllo12123321.12'str_str2_v =2×9 char 数组'hEllo12' '123321.12'name_array =4×9 char 数组'hEllo12' '123321.12' 'lo' 'hell'str_match =4NameSizeBytesClassAttributesabs_ASCII1x756double char_str1x714char comp1x11logical comp_i1x11logical comp_i_n1x11logical comp_n1x11logical dec_2_hex7x228char double_ASCII1x756double eval_double1x18double int2str_str1x3366char is_letter1x77logical is_space1x1010logical name_array4x972char pos1x18double remainder1x1174string setstr_str1x714char sprintf_str1x249498char sscanf_value1x18double str1x714char str21x918char str2num_double1x18double str31x24char str41x48char str51x1020char str_lower1x714char str_match1x18double str_rep1x1020char str_str21x1632char str_str2_v2x936char str_upper1x714char token1x1158string

字符串函数汇总: matlab|【无标题】Matlab_字符(串)操作(函数)
文章图片

matlab|【无标题】Matlab_字符(串)操作(函数)
文章图片

matlab|【无标题】Matlab_字符(串)操作(函数)
文章图片
matlab|【无标题】Matlab_字符(串)操作(函数)
文章图片


【matlab|【无标题】Matlab_字符(串)操作(函数)】

    推荐阅读