Mercurial > hg > octave-lyh
annotate scripts/strings/substr.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | 693e22af08ae |
children | 139f993936af |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1996-2011 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
2270 | 18 |
3361 | 19 ## -*- texinfo -*- |
6753 | 20 ## @deftypefn {Function File} {} substr (@var{s}, @var{offset}, @var{len}) |
3361 | 21 ## Return the substring of @var{s} which starts at character number |
6753 | 22 ## @var{offset} and is @var{len} characters long. |
3426 | 23 ## |
6753 | 24 ## If @var{offset} is negative, extraction starts that far from the end of |
25 ## the string. If @var{len} is omitted, the substring extends to the end | |
2355 | 26 ## of S. |
3426 | 27 ## |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
28 ## For example: |
3426 | 29 ## |
3361 | 30 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
31 ## @group |
3361 | 32 ## substr ("This is a test string", 6, 9) |
33 ## @result{} "is a test" | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
34 ## @end group |
3361 | 35 ## @end example |
3426 | 36 ## |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
37 ## This function is patterned after AWK@. You can get the same result by |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7411
diff
changeset
|
38 ## @code{@var{s}(@var{offset} : (@var{offset} + @var{len} - 1))}. |
3361 | 39 ## @end deftypefn |
2270 | 40 |
5428 | 41 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355 | 42 ## Adapted-By: jwe |
2314 | 43 |
2355 | 44 function t = substr (s, offset, len) |
2270 | 45 |
46 if (nargin < 2 || nargin > 3) | |
6046 | 47 print_usage (); |
2270 | 48 endif |
49 | |
5443 | 50 if (ischar (s)) |
2270 | 51 nc = columns (s); |
2355 | 52 if (abs (offset) > 0 && abs (offset) <= nc) |
6753 | 53 if (offset <= 0) |
54 offset += nc + 1; | |
2355 | 55 endif |
2270 | 56 if (nargin == 2) |
3426 | 57 eos = nc; |
2270 | 58 else |
6753 | 59 eos = offset + len - 1; |
2270 | 60 endif |
61 if (eos <= nc) | |
6753 | 62 t = s (:, offset:eos); |
2270 | 63 else |
3426 | 64 error ("substr: length = %d out of range", len); |
2270 | 65 endif |
66 else | |
2355 | 67 error ("substr: offset = %d out of range", offset); |
2270 | 68 endif |
69 else | |
70 error ("substr: expecting string argument"); | |
71 endif | |
2325 | 72 |
2270 | 73 endfunction |
7411 | 74 |
75 %!assert(strcmp (substr ("This is a test string", 6, 9), "is a test")); | |
76 | |
77 %!error substr (); | |
78 | |
79 %!error substr ("foo", 2, 3, 4); | |
80 |