TCL练习5:string、变量$的使用以及转义符何时发挥作用

目录

 

练习

string用法说明

string compare ?-nocase?  ?-length n? string1 string2

string  equal ?-nocase?  ?-length   n? string1 string2

string  first string1 string2 ?startindex?

string   index string charIndex

string    last   string1 string2   ?startindex?

string    length    string

string match ?-nocase? pattern string

string   range    string first   last

string   repeat   string   count

string    replace     string   first    last ?newstring?

string   tolower   string  ?first?   ?last?

string   toupper  string   ?first?    ?last?

string   trim    string   ?chars?

string  trimleft  string ?chars?

string   trimright    string   ?chars?


练习

练习使用string,以及$符号使用时的注意事项。由于对$的使用没搞清楚,在初期使用时犯了一些错误,在此记录以下。


set str "Hello World!! Stupid!"

if { ![string is xdigit $str] } {
    puts "str is not Hex."
}

puts [string first "orl" $str]
puts [string first "S" $str]

# Replace the "Stupid!"
if { [string match -nocase {*Stupid*} $str] } {
    puts "Are you speaking dirty language?"
    set start [string first {Stupid} $str]
    set stop [expr $start+6]
    puts $start 
    set str [string replace $str $start $stop "How are you?"]
    puts $str
}

puts [string map "Hello Hi" $str]
puts [string tolower $str]
puts [string toupper $str]

# Inverts capital
set temp ""
set l [string length $str]
for {set i 0} {$i<$l} {incr i} {
    if { [string is alpha [string index $str $i]] } {
        if { [string is lower [string index $str $i]] } {
            append temp [string toupper [string index $str $i]]
        } else {
            append temp [string tolower [string index $str $i]]
        }
    } else {
        append temp [string index $str $i]
    }
}
puts [set str $temp]

以上程序练习了string的部分使用方法,其中需要注意的有:

1. matchh在使用时,如果不使用通配符,那么pattern必须和字符串中的某一部分(空格、换行等分隔)全字匹配,否则视为未匹配。

2. $用于引用某个变量的值,如以上程序中的$i。但是如果要操作某个变量,如累加、赋值等操作,就不能使用$。以上程序中,for循环原先写成了for {set i 0} {$i<$l} {incr $i},导致后续程序工作异常。

string用法说明

string命令的option选项较多,下面介绍其中常用的部分。

string compare ?-nocase?  ?-length n? string1 string2

将字符串string1和string2进行比较,返回值为-1、0或1,分别对应string1小于、等于或大于string2。如果有 -length 参数,那么只比较前n个字符,如果n为负数,那么这个参数被忽略。 如果有 -nocase参数,那么比较时不区分大小写。比如abc比ab大,abc比abb大。

string  equal ?-nocase?  ?-length   n? string1 string2

        把字符串string1和string2进行比较,如果两者相同,返回值为1,否则返回0。

string  first string1 string2 ?startindex?

 在string2 中从头查找与string1匹配的字符序列,如果找到,那么就返回匹配的第一个字母所在的位置(0-based)。如果没有找到,那么返回-1。如果给出了startindex变量,那么将从startindex处开始查找。例如:

% string first ab  defabc

3

% string first ab  defabc  4

-1

string   index string charIndex

 返回string中第charIndex个字符(0-based)。charIndex可以是下面的值:

 整数n: 字符串中第n个字符(0-based)

 end : 最后一个字符

 end-整数n:倒数第n个字符。string index "abcd" end-1 返回字符'c'

 如果charIndex小于0,或者大于字符串string的长度,那么返回空。

% string index abcdef  4

e

% string index abcdef  end-3

c

string    last   string1 string2   ?startindex?

从后往前查找。

string    length    string

返回字符串string的长度。如string length "aaabbb"返回6。

string match ?-nocase? pattern string

如果pattern匹配string,那么返回1,否则返回0。如果有-nocase参数,那么就不区分大小写。如果pattern中没有通配符,那么必须全字匹配pattern才会返回1。如:

set a "x yz"

string match "x" $a

上述匹配会返回0。只有使用string match "x yz" $a才会返回1。

在pattern 中可以使用通配符:

  • *:匹配string中的任意长的任意字符串,包括空字符串.

  • ?:匹配string中任意单个字符

  • [chars]:匹配字符集合chars中给出的任意字符,其中可以使用 A-Z这种形式

  • \x:匹配单个字符x,使用'\'是为了让x可以为字符*,-,[,].

set s1 "\*abc"
set s2 "2abc"
puts [string match {*abc} $s1]
puts [string match {*abc} $s2]
puts [string match {\*abc} $s1]
puts [string match {\*abc} $s2]

set s1 "\[abc"
puts [string match {*abc} $s1]
puts [string match {\*abc} $s1]
puts [string match {\[abc} $s1]
puts [string match {[abc} $s1]

set s1 {\[abc}
puts [string match {*abc} $s1]
puts [string match {\*abc} $s1]
puts [string match {\[abc} $s1]
puts [string match {[abc} $s1]
puts [string match {\\\[[abc][abc][abc]} $s1]

上面代码的输出应该是:1110,1010,1000,1,下面进行分析。

在TCL中,任何命令需要一段脚本作为参数时(如果脚本使用{}或""包含起来),在执行时总是将这对{}或者""去掉,只使用其内部的脚本。

所以在string match {*abc}中即使使用了{},{}也会被去掉,*表示通配符。其中*可以匹配任何长度字符,所以上述代码的第1、2、5、9个match的返回值都是1。

string match {\*abc}中,去掉{}后*被\进行了转义,不再是通配符,而表示符号*,所以第3个match返回1,而第4个match返回0。

set s1 "\[abc"中的[被转义,不再是特殊字符,s1表示字符串[abc。string match后面的参数是一个脚本,所以即使使用了{}也会被去掉。string match {\[abc}将匹配字符串[abc,而string match {[abc}中的[没有被转义,还是特殊字符,所以第7个match返回1,第8个match返回0。

set s1 {\[abc}中,set命令的参数是字符串而不是脚本,所以此处{}能够发挥禁止转义的作用,s1是字符串\[abc。所以第9个match返回1,第10、11、12个match都返回0。

第13个match返回1。因为转义符发挥了作用,\\表示符号\,\[表示符号[,所以能够匹配。

string   range    string first   last

返回字符串string中从第first个到第last个字符的子字符串(0-based)。如果first<0,那么first被看作0,如果last大于或等于字符串的长度,那么last被看作end,如果first比last大,那么返回空。

string   repeat   string   count

返回值为:重复了string字符串count次的字符串。例如:

% string repeat "abc"  2

abcabc

string    replace     string   first    last ?newstring?

返回值为:从字符串string 中删除了第first到第last个字符(0-based)的字符串,如果给出了newstring变量,那么就用newstring替换从第first到第last个字符。如果first<0,那么first被看作0,如果last大于或等于字符串的长度,那么last被看作end,如果first比last大或者大于字符串string的长度或者last小于0,那么返回string 。

string   tolower   string  ?first?   ?last?

返回值为:把字符串string转换成小写后的字符串,如果给出了first和last变量,就只转换first和last之间的字符。

string   toupper  string   ?first?    ?last?

转换成大写。first和last参考string tolower。

string   trim    string   ?chars?

返回值为:从string字符串的首尾删除掉了字符集合chars中的字符后的字符串。如果没有给出chars,那么将删除掉spaces、tabs、newlines、carriage returns这些字符。例如:

string  trimleft  string ?chars?

类似string trim,不过只删除左边的字符。

string   trimright    string   ?chars?

类似string trimleft,不过只删除右边的字符。

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Tcl字符串正则表达式是一种用于匹配和搜索字符串的模式。Tcl正则表达式基于1003.2规范和一些Perl5扩展。它包含一个或多个约束或量化原子的连接。正则表达式可以使用不同的风格,包括扩展正则(ERE)、基本正则(BRE)和高级正则(ARE)。Tcl实现了高级正则表达式风格,它在ERE的基础上添加了一些重要的扩展。通常,正则表达式的风格由应用程序的方法指定。如果表达式以“:”开头,则剩余的表达式为高级正则表达式(ARE)。如果以“=”开头,则其余的表达式被视为普通字符。Tcl字符串正则表达式的语法为regexp ?optionalSwitches? patterns ?searchString? fullMatch subMatch1 ... subMatchn。[1][2] BRE与ERE在几个方面有所不同。在BRE中,字符“|”、“+”和“?”只是普通字符,没有与它们的功能等价的字符。边界的分隔符是“\{”和“\}”,而“{”和“}”本身为普通字符。嵌套子表达式的括号是“\(\)”和“\(\)”和“\(\)”,而“(”和“)”本身为普通字符。除了在正则表达式或圆括号子表达式的开头以外,“^”均为普通字符。除了在正则表达式或圆括号子表达式的末尾以外,“$”均为普通字符。如果“*”出现在正则表达式或圆括号子表达式的开头(或者有一个前导字符^),则为普通字符。只能使用个位数的反向引用。除了“\<”和“\>”等价于“[[:<:]]”和“[[:>:]]”之外,没有其他转义字符可用。[3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值