Updated 2013-05-23 08:00:34 by APN
string compare ?-nocase? ?-length int? string1 string2

Perform a character-by-character comparison of strings string1 and string2. Returns -1, 0, or 1, depending on whether string1 is lexicographically less than, equal to, or greater than string2. If -length is specified, then only the first length characters are used in the comparison. If -length is negative, it is ignored. If -nocase is specified, then the strings are compared in a case-insensitive manner.

Returns -1, 0, or 1, depending on whether string1 is lexicographically less than, equal to, or greater than string2. Options are -nocase or -length integer. (From the Tcl/Tk Reference Guide)

"Lexicographically less, equal, or greater" means in terms of ASCII values. The less the ASCII value, the less the character is lexicographically.

Basically it means: . < 0 < 1 < 2 < ... < 9 < A < B < ... < Z < a < b < ... < z

The command compares the strings character by character beginning at the left side (i.e. the first character) until it either finds a difference or the end of the shortest string.

For example:
   string compare "abc" "xyz"

would return a -1, because "a" is less than "z" in ASCII.
   string compare "abcd" "abc"

would return a 1 - the first string is 'greater' than the second because it is longer.

What about
   string compare "3" "10"

While one might be tempted to say "well, 3 is less than 10, so it will return a -1", the true answer is that the above returns a "1" - because lexicographically, 3 comes after 1. This is a reason NOT to use string compare if the arguments may both be numeric a nd you want to know when one is truly less than the other.

The -nocase option ignores the case of the two strings as they are compared.

The -length NUMBER compares only the first NUMBER of characters. Note that this value is one based.

See also edit