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 -*- |
|
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. |
3426
|
24 ## |
2355
|
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. |
3426
|
28 ## |
3361
|
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 ## @quotation |
|
37 ## This function is patterned after AWK. You can get the same result by |
3426
|
38 ## @code{@var{s} (@var{beg} : (@var{beg} + @var{len} - 1))}. |
3361
|
39 ## @end quotation |
|
40 ## @end deftypefn |
2270
|
41 |
5428
|
42 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355
|
43 ## Adapted-By: jwe |
2314
|
44 |
2355
|
45 function t = substr (s, offset, len) |
2270
|
46 |
|
47 if (nargin < 2 || nargin > 3) |
3456
|
48 usage ("substr (s, offset, len)"); |
2270
|
49 endif |
|
50 |
5443
|
51 if (ischar (s)) |
2270
|
52 nc = columns (s); |
2355
|
53 if (abs (offset) > 0 && abs (offset) <= nc) |
|
54 if (offset > 0) |
3426
|
55 beg = offset; |
2355
|
56 else |
3426
|
57 beg = nc + offset + 1; |
2355
|
58 endif |
2270
|
59 if (nargin == 2) |
3426
|
60 eos = nc; |
2270
|
61 else |
3426
|
62 eos = beg + len - 1; |
2270
|
63 endif |
|
64 if (eos <= nc) |
3426
|
65 t = s (:, beg:eos); |
2270
|
66 else |
3426
|
67 error ("substr: length = %d out of range", len); |
2270
|
68 endif |
|
69 else |
2355
|
70 error ("substr: offset = %d out of range", offset); |
2270
|
71 endif |
|
72 else |
|
73 error ("substr: expecting string argument"); |
|
74 endif |
2325
|
75 |
2270
|
76 endfunction |