필터 지우기
필터 지우기

Check if cell contains specific letter

조회 수: 40 (최근 30일)
jakob ekwall
jakob ekwall 2016년 1월 27일
댓글: Stephen23 2016년 1월 27일
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?

답변 (1개)

Guillaume
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
Stephen23 2016년 1월 27일
% regexp
~cellfun(@isempty,regexp(c,'i'))
% strcmp
~strcmp(strrep(c,'i','x'),c)

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by