How to create function file that makes replacements to a string?
1 次查看(过去 30 天)
显示 更早的评论
Like, if I have a string = 'Bobs-Burgers', and I want to make a function file to remove the dash to yield 'BobsBurgers', how do I do that? Do I use things like strfind/strrep? I'm just kind of confused where to start, because I feel like I have an idea but I don't know the syntax to flesh it out.
-------- My idea: --------
if string contains '-'
strrep(somethingsomething)
end
------------------------------
Any tips would be appreciated. Thank you.
回答(2 个)
Ameer Hamza
2018-5-19
编辑:Ameer Hamza
2018-5-19
The first argument of strrep is the actual char array. The second input is the char you want to replace and third is the thing with which you want to replace. Since you just want to delete the character '-', so replace it with an empty character.
strrep( 'Bobs-Burgers', '-', '')
0 个评论
Image Analyst
2018-5-19
strrep() is good. You can use that. An alternative way is to use logical indexing and set the indexes to null:
str(str=='_') = []; % Remove underlines.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!