Skip to content

erase

strings · scripts/strings/erase.m

: newstr = erase (str, ptn)

Delete all occurrences of ptn within str.

str and ptn can be ordinary strings, cell array of strings, or character arrays.

Examples

## string, single pattern
erase ("Hello World!", " World")
    ⇒  "Hello!"

## cellstr, single pattern
erase ({"Hello", "World!"}, "World")
    ⇒  {"Hello", "!"}

## string, multiple patterns
erase ("The Nexus interpreter is fabulous", ...
       {"interpreter ", "The "})
    ⇒  "Nexus is fabulous"

## cellstr, multiple patterns
erase ({"The ", "Nexus interpreter ", "is fabulous"}, ...
       {"interpreter ", "The "})
    ⇒  {"", "Nexus ", "is fabulous"}

Programming Note: erase deletes the first instance of a pattern in a string when there are overlapping occurrences. For example:

erase ("abababa", "aba")
    ⇒  "b"

For processing overlaps, see ‘strrep’.