Effacer les filtres
Effacer les filtres

Removing specific characters in a string?

313 vues (au cours des 30 derniers jours)
Nikhil Bhatia
Nikhil Bhatia le 6 Oct 2012
Commenté : Olcay le 12 Fév 2023
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 commentaires
Walter Roberson
Walter Roberson le 6 Oct 2012
What about upper case characters?
Olcay
Olcay le 12 Fév 2023
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'

Connectez-vous pour commenter.

Réponse acceptée

Matt Fig
Matt Fig le 6 Oct 2012
str = 'The quick brown fox jumped over the lazy dog';
strn = regexprep(str,'[tixy]','')

Plus de réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 6 Oct 2012
Modifié(e) : Azzi Abdelmalek le 6 Oct 2012
s='picture' % Example
s(regexp(s,'[t,i,x,y]'))=[]
  3 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 6 Oct 2012
s(regexp(s,'[tixy]'))=[]
Paul Safier
Paul Safier le 23 Juin 2022
Great solution, thanks.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 6 Oct 2012
Hint #1: ismember()
Hint #2: logical indexing
  2 commentaires
Nikhil Bhatia
Nikhil Bhatia le 6 Oct 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 le 6 Oct 2012
Modifié(e) : Walter Roberson le 24 Juin 2022
Consider
s == 't' | s == 'i' | s == 'x' | s == 'y'

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by