home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / strings / substr.m < prev    next >
Text File  |  1998-11-10  |  2KB  |  62 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. ## usage:  substr (s, offset, len)
  21. ##
  22. ## Returns the substring of S of length LEN starting at index OFFSET.
  23. ## If OFFSET is negative, extraction starts that far from the end of
  24. ## the string.  If LEN is omitted, the substring extends to the end
  25. ## of S.
  26.  
  27. ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>
  28. ## Adapted-By: jwe
  29.  
  30. function t = substr (s, offset, len)
  31.  
  32.   if (nargin < 2 || nargin > 3)
  33.     usage ("substr (s, offset [, len])");
  34.   endif
  35.  
  36.   if (isstr (s))
  37.     nc = columns (s);
  38.     if (abs (offset) > 0 && abs (offset) <= nc)
  39.       if (offset > 0)
  40.     beg = offset;
  41.       else
  42.     beg = nc + offset + 1;
  43.       endif
  44.       if (nargin == 2)
  45.     eos = nc;
  46.       else
  47.     eos = beg + len - 1;
  48.       endif
  49.       if (eos <= nc)
  50.     t = s (:, beg:eos);
  51.       else
  52.     error ("substr: length = %d out of range", len);
  53.       endif
  54.     else
  55.       error ("substr: offset = %d out of range", offset);
  56.     endif
  57.   else
  58.     error ("substr: expecting string argument");
  59.   endif
  60.  
  61. endfunction
  62.