Documentation

erase

Delete substrings within strings

Syntax

newStr = erase(str,match)

Description

example

newStr = erase(str,match)deletes all occurrences ofmatchinstr. Theerasefunction returns the rest of the string asnewStr.

Ifmatchis a string array or a cell array of character vectors, thenerasedeletes every occurrence of every element ofmatchinstr. Thestrandmatcharguments do not need to be the same size.

Examples

collapse all

创建一个字符串数组和德尔ete substrings from it. Starting in R2017a, you can create strings using double quotes instead of thestringfunction.

str = ["the quick brown fox jumps";"over the lazy dog"]
str =2x1 string array"the quick brown fox jumps" "over the lazy dog"

Delete the substring"the "fromstr. Theerasefunction deletes both instances.

newStr = erase(str,"the ")
newStr =2x1 string array"quick brown fox jumps" "over lazy dog"

Delete multiple substrings fromstr.

match = ["the ","quick ","lazy "]; newStr = erase(str,match)
newStr =2x1 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'

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.

Tips

  • To delete multiple occurrences of a match when the occurrences overlap, use thestrrepfunction.eraseonly deletes the first occurrence when occurrences overlap.

Extended Capabilities

Introduced in R2016b

Was this topic helpful?