Determining the size of a string

The size of a string can be an invaluable piece of information. Imagine if you will, loading data to a database where the field sizes are fixed at 25 characters. Trying to load a string of 50 characters into a field designed for 25 will, at best, result in a truncation of the data. Keep in mind that the byte-length of a string may be greater if multibyte characters exist. To assist us in this, Tcl has provided the length keyword.

The syntax of the string command is as follows:

	string length string

The only values passed to the string command in this instance are the length keyword and the string to be checked.

How to do it…

In the following example, we will determine the length of a character string that contains ...

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.