필터 지우기
필터 지우기

How to create function file that makes replacements to a string?

조회 수: 1 (최근 30일)
Emanuele Joy
Emanuele Joy 2018년 5월 19일
댓글: Jan 2018년 5월 21일
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
Jan
Jan 2018년 5월 19일
You mention strrep already, which is the perfect solution. Did you read doc strrep already?
Jan
Jan 2018년 5월 21일
@Emanuele Joy: Please do not close your own question, after answers have been posted. Thanks.

댓글을 달려면 로그인하십시오.

답변 (2개)

Ameer Hamza
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', '-', '')

Image Analyst
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.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by