Replacing characters in a string

37 次查看(过去 30 天)
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 个评论
Ameer Hamza
Ameer Hamza 2020-4-9
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 2020-4-9
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

请先登录,再进行评论。

回答(1 个)

Birdman
Birdman 2020-4-9
编辑:Birdman 2020-4-9
One example(use regexprep):
s="Yao Ming";
regexprep(s,{'Y','M'},{'J','Q'})

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by