轉(zhuǎn)自:http://blog.sina.com.cn/s/blog_49c07d4801000a3x.html
matlab中這兩個(gè)字符串查找的函數(shù)findstr(), strfind()表明上看起來(lái)用法相似,效果也相似。
- findstr(s1,s2)--在較長(zhǎng)的字符串中查找較短的字符串出現(xiàn)的次數(shù),并返回其位置,因此無(wú)論s1,s2哪個(gè)為長(zhǎng)字符串,位置在前在后都沒(méi)有關(guān)系。
例:
s = 'Find the starting indices of the shorter string.';
findstr(s, 'the')
ans =
6 30
findstr('the', s)
ans =
6 30
- strfind(s1,s2)--or strfind(s1,pattern),因此其意思在s1中搜索pattern,
例:
S = 'Find the starting indices of the pattern string';
strfind(S, 'in')
ans =
2 15 19 45
strfind(S, 'In')
ans =
[]
看例子似乎挺簡(jiǎn)單的,但具體用到我的例子上則有問(wèn)題,前面的findstr(s1,s2)掉換位置,提示出錯(cuò)。剛剛檢查發(fā)現(xiàn)是元胞結(jié)構(gòu)體的問(wèn)題,使用textread()讀進(jìn)來(lái)的字符串為什么成cell結(jié)構(gòu)?
通過(guò)cell2mat()可以把cell結(jié)構(gòu)轉(zhuǎn)成數(shù)據(jù),findstr(),strfind()函數(shù)只能對(duì)一維字符串?dāng)?shù)據(jù)進(jìn)行操作,若是二維字符串則得使用別的函數(shù)。