strrep
Replaces every occurrence of a substring with another.
Category: Language Fundamentals > Data Types
Syntax
s = strrep(str, old, new)
Replaces each old in str with new.
Description
The search is literal and case sensitive, scanning left to right without overlap: strrep('aaa', 'aa', 'b') is 'ba'. If old is empty or does not occur, str is returned unchanged.
For patterns, case-insensitive replacement or capture groups, use regexprep; replace is the newer function for strings.
Input arguments
str- Character vector, string array, or cell array of character vectors.
old- Literal text to find.
new- Replacement text; may be empty to delete.
Output arguments
s- Text of the same type and shape as
str.
Example
Characters, cells and overlap
strrep('hello world', 'o', '0')
strrep({'aa','ba'}, 'a', 'xy')
strrep('aaa', 'aa', 'b')
Output
ans = 'hell0 w0rld'
ans = 1×2 cell
{'xyxy'} {'bxy'}
ans = 'ba'