所以说我有一个带有一些下划线的字符串,例如hi_there。
有没有一种方法可以将字符串自动转换为“ hi there”?
(顺便说一下,原始字符串是一个变量名,我正在将其转换为绘图标题)。
令人惊讶的是,还没有人提到strrep
:
>> strrep('string_with_underscores', '_', ' ')
ans =
string with underscores
这应该是进行简单字符串替换的正式方法。对于这样一个简单的案例,regexprep
那就太过分了:是的,它们是瑞士刀,可以做所有可能的事情,但是它们附带了很长的手册。AndreasH显示的字符串索引仅适用于替换单个字符,而不能执行以下操作:
>> s = 'string*-*with*-*funny*-*separators';
>> strrep(s, '*-*', ' ')
ans =
string with funny separators
>> s(s=='*-*') = ' '
Error using ==
Matrix dimensions must agree.
另外,它也适用于带有字符串的单元格数组:
>> strrep({'This_is_a','cell_array_with','strings_with','underscores'},'_',' ')
ans =
'This is a' 'cell array with' 'strings with' 'underscores'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句