Replacing characters in a string

조회 수: 37 (최근 30일)
Patrick Crosby
Patrick Crosby 2020년 4월 9일
댓글: Patrick Crosby 2020년 4월 9일
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