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