annotate scripts/strings/substr.m @ 2270:cfcf8ff7d2dd

[project @ 1996-05-24 02:10:11 by jwe] Initial revision
author jwe
date Fri, 24 May 1996 02:10:11 +0000
parents
children 6dedd4e0a82f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2270
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
1 # Copyright (C) 1996 John W. Eaton
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
2 #
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
3 # This file is part of Octave.
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
4 #
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
5 # Octave is free software; you can redistribute it and/or modify it
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
6 # under the terms of the GNU General Public License as published by the
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
7 # Free Software Foundation; either version 2, or (at your option) any
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
8 # later version.
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
9 #
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
10 # Octave is distributed in the hope that it will be useful, but WITHOUT
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
13 # for more details.
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
14 #
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
16 # along with Octave; see the file COPYING. If not, write to the Free
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
17 # Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
18
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
19 function t = substr (s, beg, len)
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
20
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
21 # usage: substr (s, beg, len)
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
22 #
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
23 # Returns the substring of S of length LEN starting at index BEG.
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
24 # If LEN is missing, the substring extends to the end of S.
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
25
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
26 # Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>.
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
27
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
28 if (nargin < 2 || nargin > 3)
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
29 usage ("substr (s, beg, len)");
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
30 endif
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
31
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
32 if (isstr (s))
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
33 nc = columns (s);
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
34 if (beg > 0 && beg <= nc)
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
35 if (nargin == 2)
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
36 eos = nc;
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
37 else
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
38 eos = beg + len - 1;
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
39 endif
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
40 if (eos <= nc)
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
41 t = s (:, beg:eos);
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
42 else
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
43 error ("substr: length = %d out of range", len);
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
44 endif
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
45 else
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
46 error ("substr: beginning index = %d out of range", beg);
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
47 endif
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
48 else
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
49 error ("substr: expecting string argument");
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
50 endif
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
51
cfcf8ff7d2dd [project @ 1996-05-24 02:10:11 by jwe]
jwe
parents:
diff changeset
52 endfunction