필터 지우기
필터 지우기

Removing specific characters in a string?

조회 수: 313 (최근 30일)
Nikhil Bhatia
Nikhil Bhatia 2012년 10월 6일
댓글: Olcay 2023년 2월 12일
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
Walter Roberson
Walter Roberson 2012년 10월 6일
What about upper case characters?
Olcay
Olcay 2023년 2월 12일
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'

댓글을 달려면 로그인하십시오.

채택된 답변

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

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 6일
편집: Azzi Abdelmalek 2012년 10월 6일
s='picture' % Example
s(regexp(s,'[t,i,x,y]'))=[]
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 6일
s(regexp(s,'[tixy]'))=[]
Paul Safier
Paul Safier 2022년 6월 23일
Great solution, thanks.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2012년 10월 6일
Hint #1: ismember()
Hint #2: logical indexing
  댓글 수: 2
Nikhil Bhatia
Nikhil Bhatia 2012년 10월 6일
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 2012년 10월 6일
편집: Walter Roberson 2022년 6월 24일
Consider
s == 't' | s == 'i' | s == 'x' | s == 'y'

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by