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

ArgumentTypeDefaultDescription
CLASSclass namerequiredThe class to test.
ValueMeaning
alnumEach character is a Unicode letter or digit.
alphaEach character is a Unicode letter.
asciiEach character is below U+0080.
booleanA boolean of Tcl: 0, 1, true, false, yes, no, on, off, or a prefix of those words, in any case.
controlEach character is a control character.
dictA valid dict: a list with an even count of elements. Tcl 9.1 only
digitEach character is a Unicode decimal digit, also outside ASCII.
doubleA valid floating-point number, also an integer, a hex integer, nan, inf, and a value too large for a double.
entierA valid integer of any size.
falseA boolean that is false.
graphEach character prints with ink: no space and no control character.
integerA valid integer that fits in 64 bits, with optional white space around it.
listA valid list.
lowerEach character is a lowercase letter.
printEach character prints: a graph character or a space, and no control character.
punctEach character is Unicode punctuation.
spaceEach character is white space.
trueA boolean that is true.
upperEach character is an uppercase letter.
wideintegerA valid integer that fits in 64 bits.
wordcharEach character is a letter, a digit, or a connector such as _.
xdigitEach character is a hex digit: 0 to 9, a to f, or A to F.
STRINGstringrequiredThe text to test.

Options

OptionTypeDefaultSummary
-strictflagnoneMakes an empty string give 0.
-failindexvariable namenoneSets 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 NONE

Returns

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

See also