Check if cell contains specific letter

38 Ansichten (letzte 30 Tage)
jakob ekwall
jakob ekwall am 27 Jan. 2016
Kommentiert: Stephen23 am 27 Jan. 2016
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?

Antworten (1)

Guillaume
Guillaume am 27 Jan. 2016
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 Kommentar
Stephen23
Stephen23 am 27 Jan. 2016
% regexp
~cellfun(@isempty,regexp(c,'i'))
% strcmp
~strcmp(strrep(c,'i','x'),c)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by