필터 지우기
필터 지우기

using strrep multiple times

조회 수: 55 (최근 30일)
James Knight
James Knight 2019년 10월 18일
편집: Guillaume 2019년 10월 18일
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
James Knight
James Knight 2019년 10월 18일
But does that not work only with one singular output?
Thanks

댓글을 달려면 로그인하십시오.

답변 (2개)

Guillaume
Guillaume 2019년 10월 18일
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 2019년 10월 18일
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
Guillaume
Guillaume 2019년 10월 18일
편집: Guillaume 2019년 10월 18일
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.

댓글을 달려면 로그인하십시오.

카테고리

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