matlab 笔记六

% 这里的0和1是logical类型

%字符

% ''表示字符 ""表示字符串

Apple_char = 'Apple' % char字符串

Apple_string = "Apple" % string字符串

Apple_string{1}(4) % ans = 'l'

% 把string类型变成char类型访问第四个变量

class('Apple') % 显示类型

double('Apple') % 字符串变成数字 Ascii

char(double('Apple')) % 数字变成字符串 Ascii

double("Apple") %ans = NaN

length('Apple') % 5 %计算字符串的长度

length("Apple") % 1

size('Apple') % ans = 1 5

size("Apple") % ans = 1 1

majors = ["English" , "History" , "Engineering"]

%{

majors =

1×3 string 数组

"English" "History" "Engineering"

%}

balnks % 打印空格

blanks(4) %打印四个空格 ' '

string('Apple') %char --> string

string([65,74,71,69,72]) % "65" "74" "71" "69" "72"

% 合并

"hello" + " world" % ans = "hello world"

['hello',' world'] % ans = 'hello world'

["ba","bc","bd"] + " in " + majors

% "ba in English" "bc in History" "bd in Engineering"

% strlength 对char,string计算字符串的长度

strlength("Apple") % ans = 5

strlength('Apple') % ans = 5

%{

majors = ["English" , "History" , "Engineering"]\

strlength(majors)

ans =

7 7 11

%}

%strcat 把字符串连在一起对char,string都可以用

strcat('hello','-','goodbye') % 'hello-goodbye'

strcat("hello","-","goodbye") % "hello-goodbye"

%{

sprintf : 将数据格式化为字符串

sprintf('%.5f',pi) %.5f小数点后面 '3.14159'

A = 1 / eps; sprintf('%0.5e',A) '4.50360e+15'

% 0.5e 保留科学计数小数点后面五位

sprintf('%0.5g',A) % 0.5g科学计数法总数为五

sprintf('%025d',[123456]) %输出长度为25的数不够补充0

%}

% deblank 后面有删除后面的空格

test_char = ' char ';

deblank(test_char) % ' char'

% strtrim 删除两边所有空格

strtrim(test_char) % 'char'

%strip 删除要删除的字符char和string都可以删除

test_char = '------------hello-----------------';

strip(test_char,'-') % 'hello'

%erase 删除要删除的字符char和string都可以删除

test_A = 'xabcdefxxacxxxxdexxxnnhdnxxx';

erase(test_A,'x') % 'abcdefacdennhdn'

test_B = string(test_A);

erase(test_B,'x') % "abcdefacdennhdn"

erase(test_B,'xa')

% "bcdefxcxxxxdexxxnnhdnxxx" 删除和xa连接在一起的

% lower 大写变成小写

lower('ABCDEFGHIJKL') % 'abcdefghijkl'

% upper 小写变成大写

upper('abcdefghjkl') % 'ABCDEFGHJKL'

%{

word1 = 'cat'; word2 = 'cot'; word3 = 'cat11' , ;

strcmp 比较两个字符串是否相同

strcmp(word1,word2) % logical 0;

strncmp 比较两个字符串前几位是否相同

strncmp(word1,word2,2) %logical 1

%查看字符串前两位是否相等

strcmpi 忽略大小写差异比较两个字符串是否相等

word4 = 'CAT'

strcmpi(word1,word4) % logical 1

isequal 查看两个字符串相等

word5 = 'cat'

isequal(word1, word5)

%}

%strfind 查看字符串的索引string和char都能用

strfind('abcde','d') % ans = 4

strfind("abcdef","cd") % ans = 3 查看前一位的位置

strfind('abddlashbdjdjd', 'd') % ans = 3 4 10 12 14

%strrep 字符串替换想要替换字符string和char都能用

strrep('abcdefghabcdeabcde','d','x')

% ans = 'abcxefghabcxeabcxe'

%count 统计字符串的数量string和char都能用

count('abcdefghabcdeabcde','d') % ans = 3

count('abcdef','') % ans = 7 前一个和后一个两个字母之间有空格

%strtok 分隔string和char都能用

[work, rest] = strtok("hello there"," ") %按空隔分隔

[work, rest] = strtok('hello there','tl') %以t或l分割

%{

strings 建立string数组

strings(2,4)

ans =

"" "" "" ""

"" "" "" ""

%}

% contains 查看字符串中是否包含字母

%{

contains(majors,'Eng')

ans =

1×3 logical 数组

1 0 1

%}

startsWith %查看开头是否和要求相同

startsWith('English','Eng') % ans = 1

startsWith('English','ch') % ans = 0

endsWith %查看结尾是否和要求相同

endsWith('English','sh') % ans = 1

endsWith('English','ch') % ans = 0

%{

strjoin() 把单个字符串中间连接在一起用空格隔开string 和 char 都可以用

strjoin(majors)

ans =

"English History Engineering"

%}

%{

join 每一行合并string 和 char 都可以用

join([["ba","bc","bd"] ;majors])

ans =

2×1 string 数组

"ba bc bd"

"English History Engineering"

%}

isletter() %判断是否是字母

% isletter('a') ans = 1

% isletter(1) ans = 0

isspace() %判断是否有空格

isspace(' ') % ans = 1

isspace('abc') % ans = 0

ischar() %判断是否是char 类型

ischar("string") % ans = 0

ischar('char') % ans = 1

isstring() %判断是否是string类型

ischar("string") % ans = 1

ischar('char') % ans = 0

%{

num = 2

int2str(num) % 整型变成字符

num2str(pi) % 数字变成字符

str2double('3.1415') % 字符串变成浮点型

str2num('2') % 字符变成数字

%}

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值