MATLAB:如何从 2x1 或 nx1 字符串中删除字符?
MATLAB: How to delete characters from 2x1 or nx1 string?
美好的一天。
这里我有一个 2x1 的字符串:
A = ["CHAPTER 1. Random info in middle one, Random info still continues. 1";...
"CHAPTER 2. Random info in middle two. Random info still continues. 1"];
如何删除“CHAPTER #”以及 space 后面的最后一个数字和 space?这是我的尝试:
%PlanA
for n=1:2
% Delete "Chapter+Nr"
A(n,1) = erase(A,'(CHAPTER \d)');
% Delete last nr 1 at end
A(n,1) = erase(A,'\d');
end
%PlanB
A(strcmp(A, 'CHAPTER \d')) = []
我不知道为什么这不起作用?
感谢帮助
谢谢!
您可以为此使用 regexprep:
regexprep(A,'CHAPTER \d+\. (.+) \d$','')
ans =
2×1 string array
"Random info in middle one, Random info still continues."
"Random info in middle two. Random info still continues."
美好的一天。
这里我有一个 2x1 的字符串:
A = ["CHAPTER 1. Random info in middle one, Random info still continues. 1";...
"CHAPTER 2. Random info in middle two. Random info still continues. 1"];
如何删除“CHAPTER #”以及 space 后面的最后一个数字和 space?这是我的尝试:
%PlanA
for n=1:2
% Delete "Chapter+Nr"
A(n,1) = erase(A,'(CHAPTER \d)');
% Delete last nr 1 at end
A(n,1) = erase(A,'\d');
end
%PlanB
A(strcmp(A, 'CHAPTER \d')) = []
我不知道为什么这不起作用?
感谢帮助 谢谢!
您可以为此使用 regexprep:
regexprep(A,'CHAPTER \d+\. (.+) \d$','')
ans =
2×1 string array
"Random info in middle one, Random info still continues."
"Random info in middle two. Random info still continues."