7.10: Parsing and Editing Text
- Page ID
- 136675
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
Parsing means breaking text apart, identifying parts of text, or replacing parts of text. This is a very common programming task. For example, if a user enters "Add 1,2", a program may need to separate the command "Add" from the numbers 1 and 2.
Splitting Text with split
The split function divides text into smaller pieces. By default, it splits at whitespace characters, such as spaces.
% Using split to separate the words in a phrase
sentence1 = "Hey there";
words = split(sentence1)
Solution
words =
2×1 string array
"Hey"
"there"
The output is an array containing the individual words. You can also specify a delimiter. A delimiter is the character or pattern where the split should happen.
Replacing Text
The replace function replaces all occurrences of one piece of text with another.
claim = 'This is a good example';
replace(claim, 'good', 'great')
Solution
Add example text here.
MATLAB also allows you to replace a section of text between two positions or between two pieces of text.
str = "The quick brown fox jumps";
replaceBetween(str, "quick", "fox", " red ")
replaceBetween(str, 11, 15, "red")
Solution
Add example text here.
strrep Versus replace
The function strrep also replaces text. In many simple examples, strrep and replace appear to do the same thing. However, they can behave differently when repeated patterns overlap or when working with certain text types. For beginner work, replace is often easier to read, while strrep is common in older MATLAB code and in character-vector examples.
text = 'The quick brown fox';
strrep(text, 'quick', 'sly')
repeats = 'abc 2 def 22 ghi 222 jkl 2222';
using_strrep = strrep(repeats, '22', '*')
using_replace = replace(repeats, '22', '*')
Solution
Add example text here.
Erasing Text
The erase function deletes all occurrences of a pattern.
STR = "The quick brown fox";
erase(STR, "quick")
eraseBetween(STR, "quick", " fox")
eraseBetween(STR, 5, 9)
Solution
Add example text here.
Notice that eraseBetween can use either text markers or numeric positions. When using numeric positions, be careful to count characters correctly, including spaces.
Inserting Text
The functions insertAfter and insertBefore add new text into an existing piece of text.
STR = 'peppers and onions';
insertAfter(STR, 'peppers ', 'mushrooms ')
insertAfter(STR, 5, 'mushrooms ')
Solution
Add example text here.
STR = 'peppers and onions';
insertBefore(STR, 'and', 'mushrooms ')
insertBefore(STR, 9, 'mushrooms ')
Solution
Add example text here.
When using positions in a character vector, count carefully. When possible, using words or delimiters as markers can make your code easier to understand.


