Replace character with another

36 views (last 30 days)
Hello.
Which command should I use in order to replace one character with another?
(For example in the word: Big, I would like to replace character i with a.)

Accepted Answer

Walter Roberson
Walter Roberson on 6 Dec 2020
S = 'Big'
S = 'Big'
S(S == 'i') = 'a'
S = 'Bag'
T = 'Big'
T = 'Big'
T = strrep(T, 'i', 'a')
T = 'Bag'
U = 'Big'
U = 'Big'
U = regexprep(U, 'i', 'a')
U = 'Bag'
  6 Comments
Walter Roberson
Walter Roberson on 6 Dec 2020
Keep in mind that regexprep() is case sensitive by default.
Ameer Hamza
Ameer Hamza on 7 Dec 2020
Note that, regexprep() might create "unexpected" result, for example,
>> out = regexprep(str,{'i','a'},{'a','e'})
out =
'Oel'
Depending on what you want, this might be the required outcome. But in such situation, I prefer replace()
>> out = replace(str,{'i','a'},{'a','e'})
out =
'Oal'

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!