Replacing characters in a string

I want to replace characters in a string using loops and if statements. For example if there is a Y in the string, I want to change it to a J. If there is an M in the string I want to change it to a Q.

2 comentarios

Ameer Hamza
Ameer Hamza el 9 de Abr. de 2020
Why for loop? Why not some built-in function. This seems like a homework question. Can you show us the code you already tried?
Patrick Crosby
Patrick Crosby el 9 de Abr. de 2020
The way I was thinking of it would be using loops-- is there an easier way? I am unfamiliar with a built-in function for this

Iniciar sesión para comentar.

Respuestas (1)

Birdman
Birdman el 9 de Abr. de 2020
Editada: Birdman el 9 de Abr. de 2020
One example(use regexprep):
s="Yao Ming";
regexprep(s,{'Y','M'},{'J','Q'})

Categorías

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

Preguntada:

el 9 de Abr. de 2020

Comentada:

el 9 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by