Removing specific characters in a string?
Mostra commenti meno recenti
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 Commenti
Walter Roberson
il 6 Ott 2012
What about upper case characters?
str = 'The quick brown fox jumped over the lazy dog';
unwanted = 'tixy';
str(~ismember(str, unwanted))
Risposta accettata
Più risposte (2)
Azzi Abdelmalek
il 6 Ott 2012
Modificato: Azzi Abdelmalek
il 6 Ott 2012
s='picture' % Example
s(regexp(s,'[t,i,x,y]'))=[]
3 Commenti
Matt Fig
il 6 Ott 2012
This will replace commas too.
Azzi Abdelmalek
il 6 Ott 2012
s(regexp(s,'[tixy]'))=[]
Paul Safier
il 23 Giu 2022
Great solution, thanks.
Walter Roberson
il 6 Ott 2012
1 voto
Hint #1: ismember()
Hint #2: logical indexing
2 Commenti
Nikhil Bhatia
il 6 Ott 2012
Walter Roberson
il 6 Ott 2012
Modificato: Walter Roberson
il 24 Giu 2022
Consider
s == 't' | s == 'i' | s == 'x' | s == 'y'
Categorie
Scopri di più su Characters and Strings in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!