2313
|
1 ## Copyright (C) 1996 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 |
|
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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2270
|
19 |
3361
|
20 ## -*- texinfo -*- |
6753
|
21 ## @deftypefn {Function File} {} substr (@var{s}, @var{offset}, @var{len}) |
3361
|
22 ## Return the substring of @var{s} which starts at character number |
6753
|
23 ## @var{offset} and is @var{len} characters long. |
3426
|
24 ## |
6753
|
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 |
2355
|
27 ## of S. |
3426
|
28 ## |
6753
|
29 ## For example, |
3426
|
30 ## |
3361
|
31 ## @example |
|
32 ## substr ("This is a test string", 6, 9) |
|
33 ## @result{} "is a test" |
|
34 ## @end example |
3426
|
35 ## |
3361
|
36 ## This function is patterned after AWK. You can get the same result by |
6753
|
37 ## @code{@var{s} (@var{offset} : (@var{offset} + @var{len} - 1))}. |
3361
|
38 ## @end deftypefn |
2270
|
39 |
5428
|
40 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355
|
41 ## Adapted-By: jwe |
2314
|
42 |
2355
|
43 function t = substr (s, offset, len) |
2270
|
44 |
|
45 if (nargin < 2 || nargin > 3) |
6046
|
46 print_usage (); |
2270
|
47 endif |
|
48 |
5443
|
49 if (ischar (s)) |
2270
|
50 nc = columns (s); |
2355
|
51 if (abs (offset) > 0 && abs (offset) <= nc) |
6753
|
52 if (offset <= 0) |
|
53 offset += nc + 1; |
2355
|
54 endif |
2270
|
55 if (nargin == 2) |
3426
|
56 eos = nc; |
2270
|
57 else |
6753
|
58 eos = offset + len - 1; |
2270
|
59 endif |
|
60 if (eos <= nc) |
6753
|
61 t = s (:, offset:eos); |
2270
|
62 else |
3426
|
63 error ("substr: length = %d out of range", len); |
2270
|
64 endif |
|
65 else |
2355
|
66 error ("substr: offset = %d out of range", offset); |
2270
|
67 endif |
|
68 else |
|
69 error ("substr: expecting string argument"); |
|
70 endif |
2325
|
71 |
2270
|
72 endfunction |