필터 지우기
필터 지우기

detecting the existence of alphabetical elements

조회 수: 13 (최근 30일)
Patrick Mboma
Patrick Mboma 2012년 10월 1일
Dear all, I would like to detect whether a string contains an alphabetical letter. A simple way of solving this problem is writing a function such as. But is there any better, more concise and faster way? Can one use, say, regular expressions and if so how?
thanks, Pat.
function flag=detect_alphabet(mystring)
referenceSet={'a','b','c','d',...,'z'};
flag=false;
for ii=1:length(mystring)
if ismember(mystring(ii),referenceSet)
flag=true;
break
end
end

채택된 답변

Matt Fig
Matt Fig 2012년 10월 1일
편집: Matt Fig 2012년 10월 1일
Perhaps best of all:
str = 'adsf2342adsfwerreoj9f3f';
str2 = '23423';
flag = any(isletter(str))
flag = any(isletter(str2))

추가 답변 (4개)

Oleg Komarov
Oleg Komarov 2012년 10월 1일
regexpr(str,'[a-zA-Z]')

Matt Fig
Matt Fig 2012년 10월 1일
편집: Matt Fig 2012년 10월 1일
str = 'adsf2342adsfwerreoj9f3f';
str2 = '23423';
flag = ~isempty(regexp(str,'[A-Za-z]'))
flag = ~isempty(regexp(str2,'[A-Za-z]'))

Jan
Jan 2012년 10월 1일
편집: Jan 2012년 10월 1일
flag = any(ismember(mystring, 'a':'z'))
But now I'm lost in the incompatibilities of Matlab versions. Perhaps it must now be:
flag = any(ismember(mystring, char('a':'z')))
Alternatively:
flag = any(mystring >= 'a' & mystring <= 'z')
Or:
flag = any(isstrprop(mystring, 'lower'))
In all cases a loop is not required.
  댓글 수: 1
Matt Fig
Matt Fig 2012년 10월 1일
flag = any((str>='A' & str<='Z') | ((str>='a' & str<='z')))

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


Patrick Mboma
Patrick Mboma 2012년 10월 1일
Dear all, thank you SO much for a prompt reply. I now have many suggestions but don't know which one is the best. I will test them all and see, which one works best.
  댓글 수: 1
Image Analyst
Image Analyst 2012년 10월 1일
Well for what it's worth, I was going to answer isletter() until Matt beat me to it.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by