如何找到在MATLAB字符串数组的子串? [英] How to find a substring in an array of strings in matlab?

查看:259
本文介绍了如何找到在MATLAB字符串数组的子串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串ADSL。我想找到这个字符串的字符串数组字符('PSTN,ADSL,ADSL,VDSL,FTTH,VDSL')

I have a string 'ADSL'. I want to find this string in an array of strings char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL')

当我运行此命令

strmatch('ADSL',char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL'));

输出 2
但是,我希望输出 [1〜2]

strmatch仅如果搜索字符串出现在一行的开头给出了积极的结果。

strmatch only gives positive result if the search string appears at the begining of row.

我如何才能找到搜索字符串,如果行的任意位置出现呢?

How can I find the search string if it occurs anywhere in the row?

推荐答案

由于以下输入:

array = {'PSTN,ADSL', 'ADSL,VDSL', 'FTTH,VDSL'};
str = 'ADSL';

我们发现使用每个字符串匹配的起始位置:

We find the starting position of each string match using:

>> pos = strfind(array, str)
pos = 
    [6]    [1]    []

>> pos = regexp(array, str)
pos = 
    [6]    [1]    []

我们可以使用,然后找到匹配的字符串的索引:

We can then find the indices of matching strings using:

>> matches = find(~cellfun(@isempty,pos))
matches =
     1     2

这篇关于如何找到在MATLAB字符串数组的子串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆