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.

 Akzeptierte Antwort

KSSV
KSSV am 6 Mai 2021

0 Stimmen

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

4 Kommentare

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Ezma Nasr
Ezma Nasr am 15 Feb. 2023

0 Stimmen

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

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020b

Gefragt:

am 6 Mai 2021

Beantwortet:

am 15 Feb. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by