7017
|
1 ## Copyright (C) 1996, 1999, 2000, 2004, 2005, 2006, 2007 Kurt Hornik |
2325
|
2 ## |
2313
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
2313
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
2270
|
18 |
3361
|
19 ## -*- texinfo -*- |
6753
|
20 ## @deftypefn {Function File} {} substr (@var{s}, @var{offset}, @var{len}) |
3361
|
21 ## Return the substring of @var{s} which starts at character number |
6753
|
22 ## @var{offset} and is @var{len} characters long. |
3426
|
23 ## |
6753
|
24 ## If @var{offset} is negative, extraction starts that far from the end of |
|
25 ## the string. If @var{len} is omitted, the substring extends to the end |
2355
|
26 ## of S. |
3426
|
27 ## |
6753
|
28 ## For example, |
3426
|
29 ## |
3361
|
30 ## @example |
|
31 ## substr ("This is a test string", 6, 9) |
|
32 ## @result{} "is a test" |
|
33 ## @end example |
3426
|
34 ## |
3361
|
35 ## This function is patterned after AWK. You can get the same result by |
6753
|
36 ## @code{@var{s} (@var{offset} : (@var{offset} + @var{len} - 1))}. |
3361
|
37 ## @end deftypefn |
2270
|
38 |
5428
|
39 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355
|
40 ## Adapted-By: jwe |
2314
|
41 |
2355
|
42 function t = substr (s, offset, len) |
2270
|
43 |
|
44 if (nargin < 2 || nargin > 3) |
6046
|
45 print_usage (); |
2270
|
46 endif |
|
47 |
5443
|
48 if (ischar (s)) |
2270
|
49 nc = columns (s); |
2355
|
50 if (abs (offset) > 0 && abs (offset) <= nc) |
6753
|
51 if (offset <= 0) |
|
52 offset += nc + 1; |
2355
|
53 endif |
2270
|
54 if (nargin == 2) |
3426
|
55 eos = nc; |
2270
|
56 else |
6753
|
57 eos = offset + len - 1; |
2270
|
58 endif |
|
59 if (eos <= nc) |
6753
|
60 t = s (:, offset:eos); |
2270
|
61 else |
3426
|
62 error ("substr: length = %d out of range", len); |
2270
|
63 endif |
|
64 else |
2355
|
65 error ("substr: offset = %d out of range", offset); |
2270
|
66 endif |
|
67 else |
|
68 error ("substr: expecting string argument"); |
|
69 endif |
2325
|
70 |
2270
|
71 endfunction |