Remove a string from another string

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 ExchangeCharacters 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