zoukankan      html  css  js  c++  java
  • findstr()与strfind()的区别

    matlab中这两个字符串查找的函数findstr(), strfind()表明上看起来用法相似,效果也相似。

    1. findstr(s1,s2)--在较长的字符串中查找较短的字符串出现的次数,并返回其位置,因此无论s1,s2哪个为长字符串,位置在前在后都没有关系。

    例:

    s = 'Find the starting indices of the shorter string.';

    findstr(s, 'the')
    ans =
    6  30

    findstr('the', s)
    ans =
    6  30

    2. 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 =
    []

    看例子似乎挺简单的,但具体用到我的例子上则有问题,前面的findstr(s1,s2)掉换位置,提示出错。刚刚检查发现是元胞结构体的问题,使用textread()读进来的字符串为什么成cell结构?

    通过cell2mat()可以把cell结构转成数据,findstr(),strfind()函数只能对一维字符串数据进行操作,若是二维字符串则得使用别的函数。

  • 相关阅读:
    数字三角形W(加强版) codevs 2189
    数字三角形W(加强版) codevs 2189
    线段树模板
    树状数组模板 洛谷3374
    洛谷 2327 [SCOI2005]扫雷
    洛谷 2327 [SCOI2005]扫雷
    洛谷1144 最短路计数
    洛谷1144 最短路计数
    洛谷1346 电车
    洛谷1346 电车
  • 原文地址:https://www.cnblogs.com/AI-Algorithms/p/3669864.html
Copyright © 2011-2022 走看看