How to replace multiple strings with another string

7 vues (au cours des 30 derniers jours)
Nadia Quli
Nadia Quli le 18 Août 2016
Commenté : Pearl le 25 Jan 2019
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!

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 18 Août 2016
Modifié(e) : Azzi Abdelmalek le 18 Août 2016
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 commentaires
HIRAKJYOTI BASUMATARY
HIRAKJYOTI BASUMATARY le 21 Jan 2018
@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 le 25 Jan 2019
I would use regexprep
aa='robotic robot';
out = regexprep(aa,'robot$','robotic');

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Workspace Variables and MAT Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by