大家想要删除字符数组或者字符串数组的首尾字符时首先想到的就是strtrim函数,但是这个函数有限制,就是其只能删除首尾的空白字符,而当我们想要删除其他特定字符时,就无能为力了。
例如如下代码,strtrim函数很好的删除了(连续)空白字符,但是不能删除特定字符,比如星号等等。
% Matlab
stra = ' hello world ';
strb = " hello world ";
strc = " hello world *";
Fa = strtrim(stra);
Fb = strtrim(strb);
Fc = strtrim(strc);
% strtrim does not remove significant whitespace characters.
% For example, strtrim removes leading and trailing space and tab characters,
% but does not remove the nonbreaking space character, char(160).
strd = ['string' char(160)];
disp(strd)
Fd = strtrim(strd);
disp(Fd)
disp(strcmp(Fd,strd))
那么如果我们想要删除首尾特定字符该怎么办呢?小编发现了一个很好用的函数,就是strip函数。该函数可以指定删除那一侧的特定字符,具体操作看如下代码:
% Matlab
stra = '* string ';
strb = ' string *';
strc = " string ^";
strd = "