Delete substrings within strings
collapse all in page
Syntax
newStr = erase(str,match)
Description
newStr = erase(str,match)
deletes all occurrences of match
in str
. The erase
function returns the remaining text as newStr
.
If match
is an array, then erase
deletes every occurrence of every element of match
in str
. The str
and match
arguments do not need to be the same size.
Examples
collapse all
Delete Substrings from String Array
Open Live Script
Create a string array and delete substrings from it.
str = ["the quick brown fox jumps"; "over the lazy dog"]
str = 2x1 string "the quick brown fox jumps" "over the lazy dog"
Delete the substring "the "
from str
. The erase
function deletes both instances.
newStr = erase(str,"the ")
newStr = 2x1 string "quick brown fox jumps" "over lazy dog"
Delete multiple substrings from str
.
match = ["the ","quick ","lazy "];newStr = erase(str,match)
newStr = 2x1 string "brown fox jumps" "over dog"
Delete File Paths Using Pattern
Open Live Script
Create a string array of file names, including full paths.
str = ["C:\Temp\MyReport.docx"; "C:\Data\Experiment1\Trial1\Sample1.csv"; "C:\Temp\Slides.pptx"]
str = 3x1 string "C:\Temp\MyReport.docx" "C:\Data\Experiment1\Trial1\Sample1.csv" "C:\Temp\Slides.pptx"
Delete the paths, leaving only file names. To match paths, create a pattern using the wildcardPattern
function that matches all text that includes a final "\"
character. Use that pattern with the erase
function.
match = wildcardPattern + "\"
match = pattern Matching: wildcardPattern + "\"
filenames = erase(str,match)
filenames = 3x1 string "MyReport.docx" "Sample1.csv" "Slides.pptx"
For a list of functions that create pattern objects, see pattern
.
Delete Substring from Character Vector
Open Live Script
Create a character vector. Delete the substring, ' World'
, including the space character.
chr = 'Hello World'
chr = 'Hello World'
newChr = erase(chr,' World')
newChr = 'Hello'
Input Arguments
collapse all
str
— Input text
string array | character vector | cell array of character vectors
Input text, specified as a string array, character vector, or cell array of character vectors.
match
— Text to delete
string array | character vector | cell array of character vectors | pattern
array
Text to delete, specified as one of the following:
String array
Character vector
Cell array of character vectors
pattern
array
Tips
To delete multiple occurrences of a match when theoccurrences overlap, use the
strrep
function.erase
onlydeletes the first occurrence when occurrences overlap.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
Usage notes and limitations:
Pattern objects are not supported.
For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
str
andmatch
must be a string scalar, a character vector, or a cell array containing not more than one character vector.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
match
must be a string array, character vector, or a cell array of character vectors.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2016b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Contact your local office