我们有一组子字符串t = (b6,b7,y7,y8)
和一个主字符串K ='hgtb6ju\u'
。
我需要检查t
中是否有K
中的任何元素。如果是,那哪个子串。
答案 0 :(得分:1)
您可以使用strfind
:
t = {'b6', 'b7', 'y7', 'y8', 'ju'};
K = 'hgtb6ju\u';
indexes = find(cellfun(@(x) ~isempty(strfind(K, x)), t));
% indexes == [1, 5] - means: 'b6' and 'ju'
isAny = ~isempty(indexes);
答案 1 :(得分:1)
比strfind解决方案短一点(如果您可以访问R2016b):
K = 'hgtb6ju\u';
t = {'b6', 'b7', 'y7', 'y8', 'ju'};
indices = find(cellfun(@(s) contains(K,s),t));
您甚至可以直接拨打contains(K,t)
,但它只会返回一个标量逻辑,指示t
的任何元素是否在K
中,而不是告诉你哪个。这就是以上的手机短信调用。
答案 2 :(得分:1)
t = {'b6', 'b7', 'y7', 'y8', 'ju'};
K = 'hgtb6ju\u';
logidx = ~cellfun(@isempty,regexp(K,t)); %Finding if substrings are present
matched = t(logidx) % Finding which substrings are present