Mercurial > hg > octave-nkf
annotate scripts/strings/substr.m @ 9209:923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
spellchecked all .txi and .texi files.
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sun, 17 May 2009 12:18:06 -0700 |
parents | 1bf0ce0930be |
children | be55736a0783 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1996, 1999, 2000, 2004, 2005, 2006, 2007, 2008, |
2 ## 2009 Kurt Hornik | |
2325 | 3 ## |
2313 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
2270 | 19 |
3361 | 20 ## -*- texinfo -*- |
6753 | 21 ## @deftypefn {Function File} {} substr (@var{s}, @var{offset}, @var{len}) |
3361 | 22 ## Return the substring of @var{s} which starts at character number |
6753 | 23 ## @var{offset} and is @var{len} characters long. |
3426 | 24 ## |
6753 | 25 ## If @var{offset} is negative, extraction starts that far from the end of |
26 ## the string. If @var{len} is omitted, the substring extends to the end | |
2355 | 27 ## of S. |
3426 | 28 ## |
6753 | 29 ## For example, |
3426 | 30 ## |
3361 | 31 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
32 ## @group |
3361 | 33 ## substr ("This is a test string", 6, 9) |
34 ## @result{} "is a test" | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
35 ## @end group |
3361 | 36 ## @end example |
3426 | 37 ## |
3361 | 38 ## 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
|
39 ## @code{@var{s}(@var{offset} : (@var{offset} + @var{len} - 1))}. |
3361 | 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) | |
6046 | 48 print_usage (); |
2270 | 49 endif |
50 | |
5443 | 51 if (ischar (s)) |
2270 | 52 nc = columns (s); |
2355 | 53 if (abs (offset) > 0 && abs (offset) <= nc) |
6753 | 54 if (offset <= 0) |
55 offset += nc + 1; | |
2355 | 56 endif |
2270 | 57 if (nargin == 2) |
3426 | 58 eos = nc; |
2270 | 59 else |
6753 | 60 eos = offset + len - 1; |
2270 | 61 endif |
62 if (eos <= nc) | |
6753 | 63 t = s (:, offset:eos); |
2270 | 64 else |
3426 | 65 error ("substr: length = %d out of range", len); |
2270 | 66 endif |
67 else | |
2355 | 68 error ("substr: offset = %d out of range", offset); |
2270 | 69 endif |
70 else | |
71 error ("substr: expecting string argument"); | |
72 endif | |
2325 | 73 |
2270 | 74 endfunction |
7411 | 75 |
76 %!assert(strcmp (substr ("This is a test string", 6, 9), "is a test")); | |
77 | |
78 %!error substr (); | |
79 | |
80 %!error substr ("foo", 2, 3, 4); | |
81 |