Replace character with another

조회 수: 58 (최근 30일)
Ivan Mich
Ivan Mich 2020년 12월 6일
댓글: Ameer Hamza 2020년 12월 7일
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.)

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by