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

    %% Replace Substrings Between Start and End Positions
% Create string arrays and replace substrings between start and end
% positions that are specified as numbers.
%
% Create a string that contains a name. To replace the middle name, specify 
% the seventh and 11th positions in the string.
str = string('Edgar Allen Poe')

%%
newStr = replaceBetween(str,7,11,'A.')
%%
% Replace substrings from each element of a string array. When you specify 
% different start and end positions with numeric arrays, they must be the same 
% size as the input string array. The replacement text also must be in a
% string array or a cell array of the same size.

str = string({'Edgar Allen Poe';'Louisa May Alcott'})
%%
newText = {'A.';'M.'};
newStr = replaceBetween(str,[7;8],[11;10],newText)