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 ...

Get Tcl/Tk 8.5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.