Determining the length of a string
To determine the length of a string, Tcl provides the length
keyword. The length
command will return a decimal string containing the number of bytes used to represent the value contained within the variable in memory. Please note that as UTF-8 uses one to three bytes for Unicode characters; the byte length will not be the same as the character length, in most circumstances. The syntax of length
is as follows:
string length variable
How to do it…
In the following example, we will determine the byte length of a string of characters. Return values from the commands are provided for clarity. Enter the following command:
% set input "The end is nigh" The end is nigh % string length $input 15
How it works…
As you can see in the example, the string
command has read the input and returned a value of 15
.