How to replace multiple strings with another string

8 ビュー (過去 30 日間)
Nadia Quli
Nadia Quli 2016 年 8 月 18 日
コメント済み: Pearl 2019 年 1 月 25 日
Hello! I am trying to replace multiple words in a string with a single word. For example the sentence would be Hello girl and cat and I want to change it to Hello boy and boy. I've tried to format it as s being Hello girl and cat and then strrep (s, 'girl' 'cat', 'boy')
Thank you!

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 18 日
編集済み: Azzi Abdelmalek 2016 年 8 月 18 日
str='Hello girl and cat'
out=regexprep(str,'girl|cat','boy')
%If you want to use strrep
str='Hello girl and cat'
out=strrep(str,'girl','boy')
out=strrep(out,'cat','boy')
  2 件のコメント
HIRAKJYOTI BASUMATARY
HIRAKJYOTI BASUMATARY 2018 年 1 月 21 日
@Azzi Abdelmalek Sir : if i have a string aa='robotic robot'; and if i use bb=strrep(aa,'robot','robotic') then it gives me 'roboticic robot' ; instead i want the output to be 'robotic robotic' . So Sir, how can i be able to achieve it ?
Pearl
Pearl 2019 年 1 月 25 日
I would use regexprep
aa='robotic robot';
out = regexprep(aa,'robot$','robotic');

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by