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.

 Accepted Answer

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

4 Comments

Great! That works for me.
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?
Huumh....because s is present once in the string. Need to check other ways.
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.

Sign in to comment.

More Answers (1)

Ezma Nasr
Ezma Nasr on 15 Feb 2023
str='bio-inspired';
newStr = erase(str,"-")

Categories

Products

Release

R2020b

Asked:

on 6 May 2021

Answered:

on 15 Feb 2023

Community Treasure Hunt

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

Start Hunting!