using strrep multiple times

Hi
I want to use strrep multiple times but , whats the correct way of doing this without retyping sstrep
Thanks in advance
choices= strrep(strrep(strrep(usertext, 'left', 'right'), 'up', 'down'), 'black' , 'white');

3 comentarios

Walter Roberson
Walter Roberson el 18 de Oct. de 2019
Consider regexprep instead
James Knight
James Knight el 18 de Oct. de 2019
But does that not work only with one singular output?
Thanks

Iniciar sesión para comentar.

Respuestas (2)

Guillaume
Guillaume el 18 de Oct. de 2019

3 votos

Use replace instead:
>> usertext = 'left black left right up down upside white upleft';
>> replace(usertext, {'left', 'up', 'black'}, {'right', 'down', 'white'})
ans =
'right white right right down down downside white downright'
Adam Danz
Adam Danz el 18 de Oct. de 2019

2 votos

This demo is dedicated to Kazuhisa Hashimoto.
usertext = 'up, up, down, down, left, right, left, right, b, a, start.';
s = regexprep(usertext,{'left','up','b'},{'right','down','a'})

1 comentario

Guillaume
Guillaume el 18 de Oct. de 2019
Editada: Guillaume el 18 de Oct. de 2019
Should have thought of that usertext for my answer ;)
+1 just for that.
However, be careful that some characters in the regexprep need to be escaped. e.g.
regexprep(usertext, {'.', '+'}, {',', '-'})
will lead to unexpected results. For that reason replace is safer.

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 18 de Oct. de 2019

Editada:

el 18 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by