Mercurial > hg > octave-lyh
comparison scripts/strings/substr.m @ 2355:c9f70d39255f
[project @ 1996-08-20 23:30:54 by jwe]
author | jwe |
---|---|
date | Tue, 20 Aug 1996 23:32:09 +0000 |
parents | b5568c31ee2c |
children | 4f40efa995c1 |
comparison
equal
deleted
inserted
replaced
2354:2ce6e1ec9b53 | 2355:c9f70d39255f |
---|---|
15 ## You should have received a copy of the GNU General Public License | 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 | 16 ## along with Octave; see the file COPYING. If not, write to the Free |
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA | 17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
18 ## 02111-1307, USA. | 18 ## 02111-1307, USA. |
19 | 19 |
20 ## usage: substr (s, beg, len) | 20 ## usage: substr (s, offset, len) |
21 ## | 21 ## |
22 ## Returns the substring of S of length LEN starting at index BEG. | 22 ## Returns the substring of S of length LEN starting at index OFFSET. |
23 ## If LEN is missing, the substring extends to the end of S. | 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. | |
24 | 26 |
25 ## Author: jwe | 27 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> |
28 ## Adapted-By: jwe | |
26 | 29 |
27 function t = substr (s, beg, len) | 30 function t = substr (s, offset, len) |
28 | |
29 ## Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. | |
30 | 31 |
31 if (nargin < 2 || nargin > 3) | 32 if (nargin < 2 || nargin > 3) |
32 usage ("substr (s, beg, len)"); | 33 usage ("substr (s, offset [, len])"); |
33 endif | 34 endif |
34 | 35 |
35 if (isstr (s)) | 36 if (isstr (s)) |
36 nc = columns (s); | 37 nc = columns (s); |
37 if (beg > 0 && beg <= nc) | 38 if (abs (offset) > 0 && abs (offset) <= nc) |
39 if (offset > 0) | |
40 beg = offset; | |
41 else | |
42 beg = nc + offset + 1; | |
43 endif | |
38 if (nargin == 2) | 44 if (nargin == 2) |
39 eos = nc; | 45 eos = nc; |
40 else | 46 else |
41 eos = beg + len - 1; | 47 eos = beg + len - 1; |
42 endif | 48 endif |
44 t = s (:, beg:eos); | 50 t = s (:, beg:eos); |
45 else | 51 else |
46 error ("substr: length = %d out of range", len); | 52 error ("substr: length = %d out of range", len); |
47 endif | 53 endif |
48 else | 54 else |
49 error ("substr: beginning index = %d out of range", beg); | 55 error ("substr: offset = %d out of range", offset); |
50 endif | 56 endif |
51 else | 57 else |
52 error ("substr: expecting string argument"); | 58 error ("substr: expecting string argument"); |
53 endif | 59 endif |
54 | 60 |