erase

Delete substrings within strings

Syntax

Description

example

newStr = erase(str,match) deletes all occurrences of match in str. The erase function returns the rest of the string as newStr.

If match is an array that contains multiple pieces of text, then erase deletes every occurrence of every piece of text specified by match. The str and match arguments do not need to be the same size.

Examples

collapse all

Create a string array and delete substrings from it. First, delete the substring 'the ' from str. The erase function deletes both instances.

str = string({'the quick brown fox jumps';
              'over the lazy dog'})
str = 

  2×1 string array

    "the quick brown fox jumps"
    "over the lazy dog"

newStr = erase(str,'the ')
newStr = 

  2×1 string array

    "quick brown fox jumps"
    "over lazy dog"

Delete multiple substrings from str.

match = {'the ','quick ','lazy '};
newStr = erase(str,match)
newStr = 

  2×1 string array

    "brown fox jumps"
    "over dog"

Create a character vector. Delete the substring, ' World', including the space character.

chr = 'Hello World'
chr =

Hello World

newChr = erase(chr,' World')
newChr =

Hello

Related Examples

Input Arguments

collapse all

Input text, specified as a string array, a character vector, or a cell array of character vectors.

Text to delete, specified as a string array, a character vector, or a cell array of character vectors.

More About

collapse all

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

Tips

  • To delete multiple occurrences of a match when the occurrences overlap, use the strrep function. erase only deletes the first occurrence when occurrences overlap.

Introduced in R2016b

Was this topic helpful?