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');

回答(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