Replacing characters in a string

46 views (last 30 days)
Patrick Crosby
Patrick Crosby on 9 Apr 2020
Commented: Patrick Crosby on 9 Apr 2020
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 Comments
Ameer Hamza
Ameer Hamza on 9 Apr 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 on 9 Apr 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

Sign in to comment.

Answers (1)

Birdman
Birdman on 9 Apr 2020
Edited: Birdman on 9 Apr 2020
One example(use regexprep):
s="Yao Ming";
regexprep(s,{'Y','M'},{'J','Q'})

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!