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 |
2355
|
20 ## usage: substr (s, offset, len) |
2311
|
21 ## |
2355
|
22 ## Returns the substring of S of length LEN starting at index OFFSET. |
|
23 ## If OFFSET is negative, extraction starts that far from the end of |
|
24 ## the string. If LEN is omitted, the substring extends to the end |
|
25 ## of S. |
2270
|
26 |
2355
|
27 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> |
|
28 ## Adapted-By: jwe |
2314
|
29 |
2355
|
30 function t = substr (s, offset, len) |
2270
|
31 |
|
32 if (nargin < 2 || nargin > 3) |
2355
|
33 usage ("substr (s, offset [, len])"); |
2270
|
34 endif |
|
35 |
|
36 if (isstr (s)) |
|
37 nc = columns (s); |
2355
|
38 if (abs (offset) > 0 && abs (offset) <= nc) |
|
39 if (offset > 0) |
|
40 beg = offset; |
|
41 else |
|
42 beg = nc + offset + 1; |
|
43 endif |
2270
|
44 if (nargin == 2) |
|
45 eos = nc; |
|
46 else |
|
47 eos = beg + len - 1; |
|
48 endif |
|
49 if (eos <= nc) |
|
50 t = s (:, beg:eos); |
|
51 else |
|
52 error ("substr: length = %d out of range", len); |
|
53 endif |
|
54 else |
2355
|
55 error ("substr: offset = %d out of range", offset); |
2270
|
56 endif |
|
57 else |
|
58 error ("substr: expecting string argument"); |
|
59 endif |
2325
|
60 |
2270
|
61 endfunction |