gpt4 book ai didi

string - 将下划线转换为Matlab字符串中的空格?

转载 作者:行者123 更新时间:2023-12-02 05:30:32 27 4
gpt4 key购买 nike

所以说我有一个带有一些下划线的字符串,例如hi_there。

有没有一种方法可以将字符串自动转换为“hi there”?

(顺便说一下,原始字符串是一个变量名,我正在将其转换为绘图标题)。

最佳答案

令人惊讶的是,还没有人提到strrep:

>> strrep('string_with_underscores', '_', ' ')
ans =
string with underscores

应该是 official way来进行简单的字符串替换。对于这种简单的情况, 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'

关于string - 将下划线转换为Matlab字符串中的空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18554860/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com