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 CenterFile Exchange에서 Workspace 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