《TCL 教程英文版》 笔记 §20 字符串比较- com..
时间:2008-08-27 来源:oychw
§20 字符串比较- compare match first last wordend
这6个字符串子命令进行字符串和模式匹配。功能没有正则表达式强大,不过执行。比正则表达式要快。
string compare string1 string2
· -1 ..... If string1 is less than string2
· 0 ........ If string1 is equal to string2
· 1 ........ If string1 is greater than string2
string first string1 string2
Returns the index of the character in string1 that starts the first match to string2, or -1 if there is no match.
string last string1 string2
Returns the index of the character in string1 that starts the last match to string2, 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 of string. 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 character just before the first one in the word which contains the index'th character of string. 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 pattern matches string. The pattern is a glob style pattern.
实例:
# cat ch20
# !/bin/bash
# \
exec tclsh "$0" "$@"
set fullpath "/usr/home/clif/TCL_STUFF/TclTutor/Lsn.17"
set relativepath "CVS/Entries"
set directorypath "/usr/bin/"
set paths [list $fullpath $relativepath $directorypath]
foreach path $paths {
set first [string first "/" $path]
set last [string last "/" $path]
# Report whether path is absolute or relative
if {$first != 0} {
puts "$path is a relative path"
} else {
puts "$path is an absolute path"
}
# If "/" is not the last character in $path, report the last word.
# else, remove the last "/", and find the next to last "/", and
# report the last word.
incr last
if {$last != [string length $path]} {
set name [string range $path $last end]
puts "The file referenced in $path is $name"
} else {
incr last -2;
set tmp [string range $path 0 $last]
set last [string last "/" $tmp]
incr last;
set name [string range $tmp $last end]
puts "The final directory in $path is $name"
}
# CVS is a directory created by the CVS source code control system.
#
if {[string match "*CVS*" $path]} {
puts "$path is part of the source code control tree"
}
# Compare to "a" to determine whether the first char is upper or lower case
set comparison [string compare $name "a"]
if {$comparison >= 0} {
puts "$name starts with a lowercase letter\n"
} else {
puts "$name starts with an uppercase letter\n"
}
}
执行结果:
./ch20
/usr/home/clif/TCL_STUFF/TclTutor/Lsn.17 is an absolute path
The file referenced in /usr/home/clif/TCL_STUFF/TclTutor/Lsn.17 is Lsn.17
Lsn.17 starts with an uppercase letter
CVS/Entries is a relative path
The file referenced in CVS/Entries is Entries
CVS/Entries is part of the source code control tree
Entries starts with an uppercase letter
/usr/bin/ is an absolute path
The final directory in /usr/bin/ is bin
bin starts with a lowercase letter