Method Reference: string.replace

string: newstr = replace (str, old, new)

Replace substrings in string array.

newstr = replace (str, old, new) replaces every occurrence of the substring old with new in each element of the string array str. old and new can be string arrays, character vectors, or cell arrays of character vectors.

When old contains several substrings, new must either be a single substring, used to replace all of them, or be of the same size as old, replacing each substring of old with the corresponding element of new. All substrings are replaced in a single left to right pass over each element: at each position the substrings of old are tried in order, the first that matches is replaced, and the scan resumes past the inserted text, so replacements are not themselves re-scanned. old and new are applied to every element of str; their size need not match the size of str.

newstr is a string array of the same size as str. A substring of old that does not occur leaves the text unchanged, and missing values in str are preserved.

Source Code: string

Example: 1

replace substitutes every occurrence of a substring with another.

 files = string ({'cat.txt'; 'dog.txt'})
files =
  2x1 string array

    "cat.txt"    
    "dog.txt"
 replace (files, '.txt', '.md')
ans =
  2x1 string array

    "cat.md"    
    "dog.md"