Although a string is generally considered to be an alphanumeric character, it can also belong to a class of strings. These classes allow us to manipulate the string in a manner consistent with its class type. For example, adding 1 to the character # will produce an error or unexpected return value. To assist us in determining the class of the string, Tcl provides the is
keyword and a list of associated classes.
The syntax for the string
command is as follows:
string is class -strict -failindex variable string
When invoked with the is
keyword the command will return 1, if the class is matched.
The classes is
will check for are as follows:
Class |
Description |
---|---|
|
Any Unicode alphabetic or digit character. |
|
Any Unicode alphabetic character. |
|
Any character with a value less than u0080 (those that are in the 7-bit ASCII range). |
|
Any of the forms allowed by TCL_GetBoolean. |
|
Any Unicode control character. |
|
Any Unicode digit character. This includes characters outside of the [0-9] range. |
|
Any of the valid forms for a double in Tcl, with optional surrounding whitespace. In case of under/overflow in the value, 0 is returned and the |
|
Any of the forms allowed to Tcl_GetBoolean where the value is false. |
|
Any Unicode printing character, except space. |
|
Any of the valid string formats for a 32-bit integer value in Tcl, with optional surrounding whitespace. In case of under/overflow in the value, 0 is returned and the |
|
Any proper list structure, with optional surrounding whitespace. In case of improper list structure, 0 is returned and the |
|
Any Unicode lower case alphabet character. |
|
Any Unicode printing character, including space. |
|
Any Unicode punctuation character. |
|
Any Unicode space character. |
|
Any of the forms allowed to Tcl_GetBoolean where the value is true. |
|
Any upper case alphabet character in the Unicode character set. |
|
Any of the valid forms for a wide integer in Tcl, with optional surrounding whitespace. In case of under/overflow in the value, 0 is returned and the |
|
Any Unicode word character. Any alphanumeric character, and any Unicode connector punctuation characters, for example an underscore. |
|
Any hexadecimal digit character ([0-9A-Fa-f]). |
As you can see care has been taken to provide a full listing of the various classes of strings to cover all situations. In the following example we will determine if a string is a member of the digit
class. Return values from the commands are provided for clarity. Enter the following command:
% string is digit a
0
As you can see, string
has returned a 0
, informing us that the character supplied is not a member of the digit class. While this is a very simple implementation of this specific keyword, it is an invaluable tool to ensure that the class desired has been provided prior to utilization. Try using various classes and string values to see the various classes and how they react when given the correct or incorrect member of a class. When invoked with the is
keyword, the string
command returns 1
if the class being tested for is matched, otherwise it will return a 0
. By applying the optional strict
switch, an empty string will return 0
as opposed to 1
. This is useful to determine the existence of an empty string. If the optional failindex
switch is passed and 0
is the return value, the index where the string failed the class test will be stored in variable. If the return value is 1
, the variable will not be set.
18.226.104.27