I have two strings like 'bio-inspired' and 'bioinspired'. I want to essentially subtract one string from another and get '-' in the end. How can I delete all letters appearing in one string from another string? The erase function doesn't work for this case.

 채택된 답변

KSSV
KSSV 2021년 5월 6일

0 개 추천

str1 = 'bio-inspired' ;
str2 = 'bioinspired' ;
str=setdiff(str1,str2)
str = '-'

댓글 수: 4

Tejas
Tejas 2021년 5월 6일
Great! That works for me.
Tejas
Tejas 2021년 5월 6일
I have a problem though. This doesn't work for repeated letters. setdiff('sam','sams') gives me an empty array. Is there a way around that?
KSSV
KSSV 2021년 5월 6일
Huumh....because s is present once in the string. Need to check other ways.
Tejas
Tejas 2021년 5월 6일
I guess erase would work for that case. Both wouldn't work for words like 's-ams' and 'sam' to get '-s'. I'll figure something out for those cases.

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

추가 답변 (1개)

Ezma Nasr
Ezma Nasr 2023년 2월 15일

0 개 추천

str='bio-inspired';
newStr = erase(str,"-")

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

릴리스

R2020b

태그

질문:

2021년 5월 6일

답변:

2023년 2월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by