Inq*_*Kea 2 string matlab replace
所以说我有一个带有一些下划线的字符串,比如hi_there.
有没有办法将该字符串自动转换为"hi there"?
(顺便说一下,原始字符串是一个变量名称,我将其转换为绘图标题).
令人惊讶的是,还没有人提到strrep
:
>> strrep('string_with_underscores', '_', ' ')
ans =
string with underscores
Run Code Online (Sandbox Code Playgroud)
这应该是进行简单字符串替换的官方方式.对于这样一个简单的案例,regexprep
是矫枉过正:是的,他们是瑞士刀,可以尽一切可能,但他们有一个长手册.AndreasH显示的字符串索引仅适用于替换单个字符,它不能这样做:
>> s = 'string*-*with*-*funny*-*separators';
>> strrep(s, '*-*', ' ')
ans =
string with funny separators
>> s(s=='*-*') = ' '
Error using ==
Matrix dimensions must agree.
Run Code Online (Sandbox Code Playgroud)
作为奖励,它也适用于带字符串的单元格数组:
>> strrep({'This_is_a','cell_array_with','strings_with','underscores'},'_',' ')
ans =
'This is a' 'cell array with' 'strings with' 'underscores'
Run Code Online (Sandbox Code Playgroud)