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

    %% Search and Replace Text
% You can search for text in character arrays and string arrays, and
% replace substrings with new text. Starting in R2016b, create
% string arrays with the |string| function. Search for substrings with
% functions such as the |contains| function. Similarly, replace text in strings
% with the |replace| function, or extract text with functions
% such as |extractBetween|. You can use any of these functions with either 
% character vectors or string arrays. For
% compatibility, you can also use functions such as |strfind| and |strrep|
% with both character vectors and string arrays.
%
%% Search for Text
% Identify text in string arrays, character vectors, or cell arrays of 
% character vectors with the |contains|, |startsWith|, and |endsWith| function.
% 
% Create a string. Determine whether it contains the substring |mary|. The
% |contains| function returns a logical |1| if it finds the substring any
% place within the string.
str = string('Rosemary Jones')

%%
TF = contains(str,'mary')

%%
% You can also use the |strfind| function to find matching text. |strfind|
% returns the index of the start of each match. In this case, |strfind|
% returns |5| because the |m| in |mary| is the fifth character of |str|.
idx = strfind(str,'mary')

%%
% Find multiple matches with |strfind|. When there are multiple matches,
% |strfind| returns the indices as an array.
idx = strfind(str,'s')

%%
% Create a string array that contains many names. Determine which names
% contain the substring |Ann|. The |contains| function returns a logical
% array that has a |1| wherever |str| has an element that contains |Ann|. 
% To create a new string array that includes only the matches, index into 
% |str| with |TF|.

str = string({'Rosemary Ann Jones', 'Peter Michael Smith', 'Ann Marie Young'})

%%
TF = contains(str,'Ann')

%%
matches = str(TF)

%%
% Find the strings that begin with |Ann|.
TF = startsWith(str,'Ann');
matches = str(TF)

%%
% Similarly, the |endsWith| function find strings that end with a specified
% piece of text.
%
% You can also use the |contains|, |startsWith|, and |endsWith| functions
% to determine whether character vectors contains text.
chr = 'John Paul Jones'

%%
TF = contains(chr,'Paul')

%%
TF = endsWith(chr,'Paul')

%%
% Use the |contains| function to find text in rows of a string array.
% |census1905| contains a few rows of simulated census data for the year
% 1905. Each row contains a name, year of birth, and number of times that
% name was given in that year.

census1905 = string({'Ann Mary','1905','230';
                     'John','1905','5400';
                     'Mary','1905','4600';
                     'Maryjane','1905','304';
                     'Paul','1905','1206'});
                 
%% 
% Find the rows where the name is equal to |Mary|.
TF = (census1905(:,1) == 'Mary');
census1905(TF,:)

%%
% Find the rows where the name is a variation of |Mary| with the |contains| 
% function.

TF = contains(census1905(:,1),'Mary');
census1905(TF,:)

%% Replace Text
% You can replace text in string arrays, character vectors, or cell arrays of 
% character vectors with the |replace|  function.
%
% Create a string. Replace the substring |mary| with |anne|.
str = string('Rosemary Jones')

%%
newStr = replace(str,'mary','anne')

%%
% You can also replace text using the |strrep| function. However, the
% |replace| function is recommended.
newStr = strrep(str,'Jones','Day')

%%
% Create a string array that contains many names. 
str = string({'Rosemary Ann Jones', 'Peter Michael Smith', 'Ann Marie Young'})

%%
% Specify multiple names to replace. 
oldText = string({'Ann','Michael'});
newText = string({'Beth','John'}); 
newStr = replace(str,oldText,newText)

%%
% Replace text in a character vector. You can use |replace| and
% |replaceBetween| with character vectors, as well as with strings.
chr = 'Mercury, Gemini, Apollo'

%%
replace(chr,'Gemini','Mars')

%%
% Replace text in a string array of file names. Append the file names to
% the address of a website. The file names contain spaces, but spaces cannot be
% part of web addresses. Replace the space character, |' '|, with |%20|,
% which is the standard for web addresses.
str = string({'Financial Report.docx';
              'Quarterly 2015 Details.docx';
              'Slides.pptx'})
          
%%
newStr = replace(str,' ','%20')

%%
% Append the file names to the address of a website.
filenames = 'http://example.com/Documents/' + newStr

%% Extract Text
% Extract a substring from string arrays or character vectors with the
% |extractAfter|, |extractBefore|, and |extractBetween| functions. Use
% these functions to extract different substrings that precede, follow, or 
% occur between specified pieces of text.
%
% Create a string array that contains file names. Extract the portions of the 
% names after |C:\Temp\| with the |extractAfter| function.
str = string({'C:\Temp\MyReport.docx';
              'C:\Temp\Data\Sample1.csv';
              'C:\Temp\Slides.pptx'})
          
%%
filenames = extractAfter(str,'C:\Temp\')

%%
% Extract customer names from a string array that encodes the names within
% XML tags.
str = string({'<CustomerName>Elizabeth Day</CustomerName>';
             '<CustomerName>George Adams</CustomerName>';
             '<CustomerName>Sarah Young</CustomerName>'})

%%
names = extractBetween(str,'<CustomerName>','</CustomerName>')