- string compare ?-nocase? ?-length int? string1 string2
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 < ... < zThe 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.