TCL(Tool Command Language)学习笔记4-String

本文介绍了使用Tcl语言进行字符串操作的方法,包括获取长度、索引字符、截取子串等,并展示了如何根据路径判断操作系统类型及解析路径。
set string_1 "This is my string."
puts "\$string_1 = \"$string_1\""

;#使用string length获取字符串长度
set length_string_1 [string length $string_1]
puts "Length of \$string_1= $length_string_1 "

;#使用string index 获取指定下标的字符
set chr_string_1_index_0 [string index $string_1 0]
puts "\$string_1\[0\]=\'$chr_string_1_index_0\'"

;#使用string range获取一个子串
set substring_string_1_5_10 [string range $string_1 5 10]
puts "\$string_1\[5~10\]=\"$substring_string_1_5_10\""

;#使用string frist查找子串在串中首次出现的位置
set substring_string_1_first_s_end [string range $string_1 [string first "s" $string_1] [expr $length_string_1 - 1]]
puts "From the first \'s\' till the end is \"$substring_string_1_first_s_end\""

;#使用string last查找子串在串中末次出现的位置
set substring_string_1_last_s_end [string range $string_1 [string last "s" $string_1] [expr $length_string_1 - 1]]
puts "From the last \'s\' till the end is \"$substring_string_1_last_s_end\""

;#画分割线
puts .....................................................

;#简单应用 根据给定路径判断OS类型并对路径进行解析
set string_2 "C:\\Documents and Settings\\Administrator\\My Documents\\Tcl"
set string_2 "/home/Traveler/work/cpp/"
set first_character [string index $string_2 0]
if {$first_character >={C} && $first_character <={Z}} {
	puts "This machine is working on windows OS."
	set tree_string_2 [split $string_2 "\\"]
} elseif {$first_character=={/}} {
	puts "This machine is working on Unix OS."
	set trim_slash_from_each_side [string trim $string_2 '/']
	set tree_string_2 [split $trim_slash_from_each_side "/"]
	set string_2 [string trimright $string_2 '/']
}
foreach item_teee_string_2 $tree_string_2 {
	puts $item_teee_string_2
}
set upper_string_2 [string toupper $string_2]
set lower_string_2 [string tolower $string_2]
puts $upper_string_2
puts $lower_string_2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanzhong104204

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值