string is
Tests whether a string is of a class, such as integer or alpha.
string is CLASS ?-strict? ?-failindex VAR? STRING
Description
Gives 1 when STRING is of CLASS, else 0. For a class of characters, such as alpha or digit, each character of STRING must be in the class. For a class of values, such as integer or list, the whole STRING must be a valid value of the type.
An empty STRING gives 1 for each class with no -strict. A unique prefix of a class name is the class: string is int 12 tests integer.
The options come after CLASS and before STRING. With no word after the options, the last word is STRING: string is integer -strict tests the text -strict.
Arguments
| Argument | Type | Default | Description | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CLASS | class name | required | The class to test.
| ||||||||||||||||||||||||||||||||||||||||||||||
| STRING | string | required | The text to test. |
Options
| Option | Type | Default | Summary |
|---|---|---|---|
-strict | flag | none | Makes an empty string give 0. |
-failindex | variable name | none | Sets a variable to the index of the first character that fails the test. |
-strict
Makes an empty string give 0.
With -strict an empty STRING gives 0 for each class. A STRING that is not empty gives the same result with and without the option.
% string is integer ""
1
% string is integer -strict ""
0
% string is alpha -strict abc
1% string is integer -strict -bogus ""
bad option "-bogus": must be -strict or -failindex
-errorcode TCL LOOKUP INDEX option-failindex VAR
Sets a variable to the index of the first character that fails the test.
When the result is 0, the command sets the variable VAR to the index of the first character where the test fails. For a class of values, such as integer or list, that is the place where the value stops being valid. An empty STRING with -strict sets VAR to 0. When the result is 1, VAR does not change, and a VAR with no value stays with no value.
% string is alpha -failindex i ab1c
> set i
2
% string is integer -failindex j 12x
> set j
2
% unset -nocomplain k
> string is integer -failindex k 12
> info exists k
0% string is integer -failindex i
wrong # args: should be "string is integer ?-strict? ?-failindex var? str"
-errorcode NONEReturns
1 or 0.
Examples
% string is integer 12
1
% string is digit 12a
0
% string is double 1e5
1
% string is boolean yes
1
% string is list "a {b"
0
# Tcl 9.1 only
% string is dict {a 1}
1
# Tcl 9.1 only
% string is integer 1_000
1
# Tcl 8.6 only
% string is integer 1_000
0
Errors
# Tcl 9.1 only
% string is bogus x
bad class "bogus": must be alnum, alpha, ascii, control, boolean, dict, digit, double, entier, false, graph, integer, list, lower, print, punct, space, true, upper, wideinteger, wordchar, or xdigit
-errorcode TCL LOOKUP INDEX class
# Tcl 8.6 only
% string is dict {a 1}
bad class "dict": must be alnum, alpha, ascii, control, boolean, digit, double, entier, false, graph, integer, list, lower, print, punct, space, true, upper, wideinteger, wordchar, or xdigit
-errorcode TCL LOOKUP INDEX class
% string is integer
wrong # args: should be "string is class ?-strict? ?-failindex var? str"
-errorcode TCL WRONGARGS
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1