1 ## Copyright (C) 2008-2012 John W. Eaton
3 ## This file is part of Octave.
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} isstrprop (@var{str}, @var{prop})
21 ## Test character string properties. For example:
25 ## isstrprop ("abc123", "alpha")
26 ## @result{} [1, 1, 1, 0, 0, 0]
30 ## If @var{str} is a cell array, @code{isstrpop} is applied recursively
31 ## to each element of the cell array.
33 ## Numeric arrays are converted to character strings.
35 ## The second argument @var{prop} must be one of
39 ## True for characters that are alphabetic (letters).
43 ## True for characters that are alphabetic or digits.
46 ## True for lowercase letters.
49 ## True for uppercase letters.
52 ## True for decimal digits (0-9).
55 ## True for hexadecimal digits (@nospell{a-fA-F0-9}).
59 ## True for whitespace characters (space, formfeed, newline, carriage
60 ## return, tab, vertical tab).
63 ## True for punctuation characters (printing characters except space
64 ## or letter or digit).
67 ## True for control characters.
71 ## True for printing characters except space.
74 ## True for printing characters including space.
77 ## True for characters that are in the range of ASCII encoding.
81 ## @seealso{isalpha, isalnum, islower, isupper, isdigit, isxdigit,
82 ## isspace, ispunct, iscntrl, isgraph, isprint, isascii}
85 function retval = isstrprop (str, prop)
93 retval = isalpha (str);
94 case {"alnum", "alphanum"}
95 retval = isalnum (str);
97 retval = isascii (str);
99 retval = iscntrl (str);
101 retval = isdigit (str);
102 case {"graph", "graphic"}
103 retval = isgraph (str);
105 retval = islower (str);
107 retval = isprint (str);
109 retval = ispunct (str);
110 case {"space", "wspace"}
111 retval = isspace (str);
113 retval = isupper (str);
115 retval = isxdigit (str);
117 error ("isstrprop: invalid string property");
123 %!assert (isstrprop ("abc123", "alpha"), logical ([1, 1, 1, 0, 0, 0]))
124 %!assert (isstrprop ("abc123", "digit"), logical ([0, 0, 0, 1, 1, 1]))
125 %!assert (isstrprop ("Hello World", "wspace"), isspace ("Hello World"))
126 %!assert (isstrprop ("Hello World", "graphic"), isgraph ("Hello World"))
127 %!assert (isstrprop (char ("AbC", "123"), "upper"), logical ([1 0 1; 0 0 0]))
128 %!assert (isstrprop ({"AbC", "123"}, "lower"), {logical([0 1 0]), logical([0 0 0])})
132 %!error isstrprop ("abc123")
133 %!error isstrprop ("abc123", "alpha", "alpha")
134 %!error <invalid string property> isstrprop ("abc123", "foo")