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 日

0 投票

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 件のコメント

Ameer Hamza
Ameer Hamza 2020 年 12 月 6 日
V = 'Big';
V = replace(V, 'i', 'a')
Ivan Mich
Ivan Mich 2020 年 12 月 6 日
Ok one more question . If i want to replace more than onre characters (for example, O with e), How could I modify the previous command?
I have tried ths but it is no use:
regexprep(V,{'i','a'},{'O','e'});
Stephen23
Stephen23 2020 年 12 月 6 日
str = 'Oil';
out = regexprep(str,{'i','O'},{'a','e'})
out = 'eal'
Ivan Mich
Ivan Mich 2020 年 12 月 6 日
i have tried this :
oo=regexprep((h),{'A','i'},{'O','e'}); ,
where h is 20x1 cell, and it is not working. I mean some characters not replaced
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 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by