comparison scripts/strings/substr.m @ 6753:a5c64dad5b93

[project @ 2007-06-25 17:05:58 by jwe]
author jwe
date Mon, 25 Jun 2007 17:05:58 +0000
parents 34f96dd5441b
children 93c65f2a5668
comparison
equal deleted inserted replaced
6752:ee2ad7b5454a 6753:a5c64dad5b93
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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 ## 02110-1301, USA. 18 ## 02110-1301, USA.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} substr (@var{s}, @var{beg}, @var{len}) 21 ## @deftypefn {Function File} {} substr (@var{s}, @var{offset}, @var{len})
22 ## Return the substring of @var{s} which starts at character number 22 ## Return the substring of @var{s} which starts at character number
23 ## @var{beg} and is @var{len} characters long. 23 ## @var{offset} and is @var{len} characters long.
24 ## 24 ##
25 ## If OFFSET is negative, extraction starts that far from the end of 25 ## If @var{offset} is negative, extraction starts that far from the end of
26 ## the string. If LEN is omitted, the substring extends to the end 26 ## the string. If @var{len} is omitted, the substring extends to the end
27 ## of S. 27 ## of S.
28 ## 28 ##
29 ## For example, 29 ## For example,
30 ## 30 ##
31 ## @example 31 ## @example
32 ## substr ("This is a test string", 6, 9) 32 ## substr ("This is a test string", 6, 9)
33 ## @result{} "is a test" 33 ## @result{} "is a test"
34 ## @end example 34 ## @end example
35 ## 35 ##
36 ## @quotation
37 ## This function is patterned after AWK. You can get the same result by 36 ## This function is patterned after AWK. You can get the same result by
38 ## @code{@var{s} (@var{beg} : (@var{beg} + @var{len} - 1))}. 37 ## @code{@var{s} (@var{offset} : (@var{offset} + @var{len} - 1))}.
39 ## @end quotation
40 ## @end deftypefn 38 ## @end deftypefn
41 39
42 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> 40 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
43 ## Adapted-By: jwe 41 ## Adapted-By: jwe
44 42
49 endif 47 endif
50 48
51 if (ischar (s)) 49 if (ischar (s))
52 nc = columns (s); 50 nc = columns (s);
53 if (abs (offset) > 0 && abs (offset) <= nc) 51 if (abs (offset) > 0 && abs (offset) <= nc)
54 if (offset > 0) 52 if (offset <= 0)
55 beg = offset; 53 offset += nc + 1;
56 else
57 beg = nc + offset + 1;
58 endif 54 endif
59 if (nargin == 2) 55 if (nargin == 2)
60 eos = nc; 56 eos = nc;
61 else 57 else
62 eos = beg + len - 1; 58 eos = offset + len - 1;
63 endif 59 endif
64 if (eos <= nc) 60 if (eos <= nc)
65 t = s (:, beg:eos); 61 t = s (:, offset:eos);
66 else 62 else
67 error ("substr: length = %d out of range", len); 63 error ("substr: length = %d out of range", len);
68 endif 64 endif
69 else 65 else
70 error ("substr: offset = %d out of range", offset); 66 error ("substr: offset = %d out of range", offset);