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

    %% Select Text Before Substring
% Create string arrays and select text that occurs before substrings.
%
% Create a string. Then create a new string that occurs before the substring
% |' brown'|. The |extractBefore| function selects the text but does
% not include |' brown'| in the output.
str = string('The quick brown fox')

%%
newStr = extractBefore(str,' brown')

%%
% Create a new string array from the elements of a string array. When you specify 
% different substrings as positions, they must be contained
% in a string array or a cell array that is the same size as |str|.
str = string({'The quick brown fox jumps';'over the lazy dog'})

%%
newStr = extractBefore(str,{' brown';' dog'})
%%
% You also can specify one substring as a position that is applied to all
% elements of the input string array.