home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts.fat / strings / substr.m < prev    next >
Text File  |  1999-12-24  |  2KB  |  78 lines

  1. ## Copyright (C) 1996 Kurt Hornik
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## -*- texinfo -*-
  21. ## @deftypefn {Function File} {} substr (@var{s}, @var{beg}, @var{len})
  22. ## Return the substring of @var{s} which starts at character number
  23. ## @var{beg} and is @var{len} characters long.
  24. ## 
  25. ## If OFFSET is negative, extraction starts that far from the end of
  26. ## the string.  If LEN is omitted, the substring extends to the end
  27. ## of S.
  28. ## 
  29. ##   For example,
  30. ## 
  31. ## @example
  32. ## substr ("This is a test string", 6, 9)
  33. ##      @result{} "is a test"
  34. ## @end example
  35. ## 
  36. ## @quotation
  37. ## @strong{Note:}
  38. ## This function is patterned after AWK.  You can get the same result by
  39. ## @code{@var{s} (@var{beg} : (@var{beg} + @var{len} - 1))}.  
  40. ## @end quotation
  41. ## @end deftypefn
  42.  
  43. ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
  44. ## Adapted-By: jwe
  45.  
  46. function t = substr (s, offset, len)
  47.  
  48.   if (nargin < 2 || nargin > 3)
  49.     usage ("substr (s, offset [, len])");
  50.   endif
  51.  
  52.   if (isstr (s))
  53.     nc = columns (s);
  54.     if (abs (offset) > 0 && abs (offset) <= nc)
  55.       if (offset > 0)
  56.     beg = offset;
  57.       else
  58.     beg = nc + offset + 1;
  59.       endif
  60.       if (nargin == 2)
  61.     eos = nc;
  62.       else
  63.     eos = beg + len - 1;
  64.       endif
  65.       if (eos <= nc)
  66.     t = s (:, beg:eos);
  67.       else
  68.     error ("substr: length = %d out of range", len);
  69.       endif
  70.     else
  71.       error ("substr: offset = %d out of range", offset);
  72.     endif
  73.   else
  74.     error ("substr: expecting string argument");
  75.   endif
  76.  
  77. endfunction
  78.