This primitive is occasionally handier than juxtaposition of strings when mixed quoting is wanted, or when the aim is to return the result of a concatenation without resorting to return -level 0, and is more efficient than building a list of arguments and using join with an empty join string.
string first a 0a23456789abcdef 5
will return 10, but
string first a 0123456789abcdef 11
will return -1.
If charIndex is less than 0 or greater than or equal to the length of the string then this command returns an empty string.
In the case of boolean, true and false, if the function will return 0, then the varname will always be set to 0, due to the varied nature of a valid boolean value.
string last a 0a23456789abcdef 15
will return 10, but
string last a 0a23456789abcdef 9
will return 1.
string map {abc 1 ab 2 a 3 1 0} 1abcaababcabababc
will return the string 01321221.
Note that if an earlier key is a prefix of a later one, it will completely mask the later one. So if the previous example is reordered like this,
string map {1 0 ab 2 a 3 abc 1} 1abcaababcabababc
it will return the string 02c322c222c.
In almost all cases, you should use the string length operation (including determining the length of a Tcl byte array value). Refer to the Tcl_NumUtfChars manual entry for more details on the UTF-8 representation.
Formally, the string bytelength operation returns the content of the length field of the Tcl_Obj structure, after calling Tcl_GetString to ensure that the bytes field is populated. This is highly unlikely to be useful to Tcl scripts, as Tcl's internal encoding is not strict UTF-8, but rather a modified CESU-8 with a denormalized NUL (identical to that used in a number of places by Java's serialization mechanism) to enable basic processing with non-Unicode-aware C functions. As this representation should only ever be used by Tcl's implementation, the number of bytes used to store the representation is of very low value (except to C extension code, which has direct access for the purpose of memory management, etc.)
Compatibility note: it is likely that this subcommand will be withdrawn in a future version of Tcl. It is better to use the encoding convertto command to convert a string to a known encoding and then apply string length to that.
string length [encoding convertto utf-8 $theString]
In the specifications above, the integer value M contains no trailing whitespace and the integer value N contains no leading whitespace.
set length [string length $string] if {$length == 0} { set isPrefix 0 } else { set isPrefix [string equal -length $length $string "foobar"] }