detecting the existence of alphabetical elements
14 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
その他の回答 (4 件)
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
2012 年 10 月 1 日
flag = any((str>='A' & str<='Z') | ((str>='a' & str<='z')))
Patrick Mboma
2012 年 10 月 1 日
1 件のコメント
Image Analyst
2012 年 10 月 1 日
Well for what it's worth, I was going to answer isletter() until Matt beat me to it.
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!