Replace character with another

27 Ansichten (letzte 30 Tage)
Ivan Mich
Ivan Mich am 6 Dez. 2020
Kommentiert: Ameer Hamza am 7 Dez. 2020
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.)

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 6 Dez. 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 Kommentare
Walter Roberson
Walter Roberson am 6 Dez. 2020
Keep in mind that regexprep() is case sensitive by default.
Ameer Hamza
Ameer Hamza am 7 Dez. 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'

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by