Removing specific characters in a string?

Im having difficulty in deleting pre-specified characters from any given string. The characters that i am tryin to eliminate are 't' 'i' 'x' 'y'
I need a lot of help creating a function that would do this for any given string.
for example
modstr('picture') should return ans = ('pcure')
or
modstr('alex') should return ans = ('ale')
I would appreciate any help or hints regarding this since i've been working on this for literally 6 hours now.
Thank You.

2 Kommentare

Walter Roberson
Walter Roberson am 6 Okt. 2012
What about upper case characters?
str = 'The quick brown fox jumped over the lazy dog';
unwanted = 'tixy';
str(~ismember(str, unwanted))
ans = 'The quck brown fo jumped over he laz dog'

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Matt Fig
Matt Fig am 6 Okt. 2012

4 Stimmen

str = 'The quick brown fox jumped over the lazy dog';
strn = regexprep(str,'[tixy]','')

Weitere Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 6 Okt. 2012
Bearbeitet: Azzi Abdelmalek am 6 Okt. 2012

6 Stimmen

s='picture' % Example
s(regexp(s,'[t,i,x,y]'))=[]

3 Kommentare

Matt Fig
Matt Fig am 6 Okt. 2012
This will replace commas too.
s(regexp(s,'[tixy]'))=[]
Paul Safier
Paul Safier am 23 Jun. 2022
Great solution, thanks.

Melden Sie sich an, um zu kommentieren.

Walter Roberson
Walter Roberson am 6 Okt. 2012

1 Stimme

Hint #1: ismember()
Hint #2: logical indexing

2 Kommentare

Nikhil Bhatia
Nikhil Bhatia am 6 Okt. 2012
im sorry but i have only been working with matlab for the past 2 days ... could you please explain a little further?
Walter Roberson
Walter Roberson am 6 Okt. 2012
Bearbeitet: Walter Roberson am 24 Jun. 2022
Consider
s == 't' | s == 'i' | s == 'x' | s == 'y'

Melden Sie sich an, um zu kommentieren.

Kategorien

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

Gefragt:

am 6 Okt. 2012

Kommentiert:

am 12 Feb. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by