1## Copyright (C) 1996, 1999, 2000, 2004, 2005, 2006, 2007, 2008,
4## This file is part of Octave.
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
21## @deftypefn {Function File} {} substr (@var{s}, @var{offset}, @var{len})
22## Return the substring of @var{s} which starts at character number
23## @var{offset} and is @var{len} characters long.
25## If @var{offset} is negative, extraction starts that far from the end of
26## the string. If @var{len} is omitted, the substring extends to the end
33## substr ("This is a test string", 6, 9)
34## @result{} "is a test"
38## This function is patterned after AWK. You can get the same result by
39## @code{@var{s}(@var{offset} : (@var{offset} + @var{len} - 1))}.
42## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
45function t = substr (s, offset, len)
47 if (nargin < 2 || nargin > 3)
53 if (abs (offset) > 0 && abs (offset) <= nc)
60 eos = offset + len - 1;
63 t = s (:, offset:eos);
65 error ("substr: length = %d out of range", len);
68 error ("substr: offset = %d out of range", offset);
71 error ("substr: expecting string argument");
76%!assert(strcmp (substr ("This is a test string", 6, 9), "is a test"));
80%!error substr ("foo", 2, 3, 4);