There are 6 string subcommands that do pattern and string matching. These are relatively fast operations, certainly faster than regular expressions, albeit less powerful.
- string compare- string1- string2
- Comparesstring1tostring2and returns:- -1 ..... If string1is less thanstring2
- 0 ........ If string1is equal tostring2
- 1 ........ If string1is greater thanstring2
 These comparisons are done alphabetically, not numerically - in other words "a" is less than "b", and "10" is less than "2".
- -1 ..... If 
- string first- string1- string2
- Returns the index of the character in string1that starts the first match tostring2, or -1 if there is no match.
- string last- string1- string2
- Returns the index of the character in string1that starts the last match tostring2, or -1 if there is no match.
- string wordend- string- index
- Returns the index of the character just after the last one in the word which contains the index'th character ofstring. A word is any contiguous set of letters, numbers or underscore characters, or a single other character.
- string wordstart- string- index
- Returns the index of the first character in the word that contains the index'th character ofstring. A word is any contiguous set of letters, numbers or underscore characters, or a single other character.
- string match- pattern- string
- Returns 1 if the patternmatchesstring. Thepatternis a glob style pattern.
Example
set fullpath "/usr/home/clif/TCL_STUFF/TclTutor/Lsn.17"
set relativepath "CVS/Entries"
set paths [list $fullpath $re
set directorypath "/usr/bin/"
lativepath $directorypath]
h]
set last [strin
foreach path $paths {
set first [string first "/" $pa
tg last "/" $path]
# Report whether path is absolute or relative
se {
puts "$pa
if {$first != 0} {
puts "$path is a relative path"
} e
lth is an absolute path"
}
racter in $path, report the last word.
# else, remove the last "/"
# If "/" is not the last ch
a, and find the next to last "/", and
# report the last word.
incr last
t end]
puts "The file referenced
if {$last != [string length $path]} {
set name [string range $path $la
sin $path is $name"
} else {
incr last -2;
set tmp [string range $path 0 $last]
end]
puts "The final directory
set last [string last "/" $tmp]
incr last;
set name [string range $tmp $las
t in $path is $name"
}
# CVS is a directory created by the CVS source code control system.
#
# Compare to "a" to determine whet
if {[string match "*CVS*" $path]} {
puts "$path is part of the source code control tree"
}
n >= 0} {
puts "$name starts with
her the first char is upper or lower case
set comparison [string compare $name "a"]
if {$comparis
o a lowercase letter\n"
} else {
puts "$name starts with an uppercase letter\n"
}
}

 
 
0 Comment:
Post a Comment