home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / STRFUN.DI$ / FINDSTR.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  842 b   |  29 lines

  1. function k = findstr(s1,s2)
  2. %FINDSTR Find one string within another.
  3. %    K = FINDSTR(S1,S2) returns the starting indices of any occurrences
  4. %    of the shorter of the two strings in the longer.
  5. %    Example:
  6. %        s = 'How much wood would a woodchuck chuck?';
  7. %        findstr(s,'a')    returns  21
  8. %        findstr(s,'wood') returns  [10 23]
  9. %        findstr(s,'Wood') returns  []
  10. %        findstr(s,' ')    returns  [4 9 14 20 22 32]
  11. %    See also STRCMP.
  12.  
  13. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  14.  
  15. % Make s2 the shorter.
  16. if length(s1) < length(s2)
  17.    t = s1; s1 = s2; s2 = t;
  18. end
  19.  
  20. % Extend s1 by something as long as s2, but which doesn't match.
  21. s1 = [s1 0*s2];
  22.  
  23. % Repeatedly shorten an index vector which points to increasingly 
  24. % longer matches. 
  25. k = 1:length(s1);
  26. for i = 1:length(s2)
  27.    k = k(find(s1(k+i-1) == s2(i)));
  28. end
  29.