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 whitespace, as you might encounter in the database scenario I mentioned earlier. The return value from the command is provided for clarity. Enter the following command:


% string length "123 Any Street"
14

How it works…

As you can see, the command has returned a value of 14 to reflect not only the alphanumeric characters, but also the enclosed whitespace.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.163.250