www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ReplaceTextBetweenSubstringsExample.m

    %% Replace Text Between Substrings
% Create string arrays and replace text that occurs between substrings.
%
% Create a string. Replace the text that occurs between the substrings
% |quick| and |fox|. The |replaceBetween| function replaces the text but does
% not replace |quick| or |fox| in the output.
str = string('The quick brown fox')

%%
newStr = replaceBetween(str,'quick ',' fox','red')

%%
% Replace substrings from each element of a string array. When you specify 
% different substrings as start and end indicators, they must be contained
% in a string array or a cell array that is the same size as |str|. The 
% replacement text also must be in a string array or a cell array of the same 
% size.
str = string({'The quick brown fox jumps';'over the lazy dog'})

%%
newText = {'red';'sleeping'};
newStr = replaceBetween(str,{'quick ';'the '},{' fox';' dog'},newText)