Check if cell contains specific letter
21 ビュー (過去 30 日間)
古いコメントを表示
I'm wondering if it is possible to check if a string contains a certain letter.
I have an array with different strings and I would like to find every cell that contains a certain letter.
'hi'
'my'
'name'
'is'
How would I find the cells containing the letter i?
0 件のコメント
回答 (1 件)
Guillaume
2016 年 1 月 27 日
Many ways to do this:
c = {'hi'; 'my'; 'name'; 'is'}
lettertofind = 'i';
%method 1: plain character comparison
cellfun(@(s) any(s == lettertofind), c)
%method 2: strfind
cellfun(@(s) ~isempty(strfind(s, lettertofind)), c)
%method 3: ismember
cellfun(@(s) ismember(lettertofind, s), c)
%etc.
1 件のコメント
Stephen23
2016 年 1 月 27 日
% regexp
~cellfun(@isempty,regexp(c,'i'))
% strcmp
~strcmp(strrep(c,'i','x'),c)
参考
カテゴリ
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!