现在的位置: 首页 > 综合 > 正文

tcl/tk实例详解——string(一)

2013年12月07日 ⁄ 综合 ⁄ 共 2653字 ⁄ 字号 评论关闭
    这里对string命令中的几个子命令使用实例进行一些解释,以便于更加容易理解string命令中的各个子命令,本文仅对几个比较容易掌握的相对简单的string命令进行实例解析。分别是bytelength、length、compare、equal、range、index、first和last几个子命令。
 
    如果对这些命令还不了解,请参考:
 
    string bytelength string
    string length string
    两个命令都是返回字符串string的长度。
    把以上的两个命令放在一起比较,更能够显现出两个命令的区别。
    bytelength是计算一个字符串的字节数,而length是计算一个字符串的长度,如果字符串为标准的ASCII码组成,那么两个命令得到的结果是相同的。例如:
    % string bytelength "This is a tcltk example"
    23
    % string length "This is a tcltk example"
    23
    两者在有其它字符出现的时候就会有区别了,比如中文:
    % string bytelength "这是一个tcltk示例"
    23
    % string length "这是一个tcltk示例"
    11
    一个中文为三个字节,所以造成了以上两者的区别,在使用这两个命令的时候需要注意这个不同点。
 
    string compare ?-nocase? ?-length int? string1 string2
    命令返回两个字符串比较的结果,string1的顺序比string2靠前就返回1,反之返回-1,相等返回0。
    -nocase的意思就是不区分大小写,本文下同,不再累述。
    -length int中的int就是需要比较的前面几个字符,本文下同,不再累述。
    以下的例子比较两个字符串:
    % string compare "This is a TCLTK example" "This is a tcltk example"
    -1
    因为ASCII码T在t之后,所以从顺序上来说后面的字符串显然在前,所以返回-1,如果把两个字符串的位置互换:
    % string compare "This is a tcltk example" "This is a TCLTK example"
    1
    如果两个字符串相同则返回0:
    % string compare "This is a tcltk example" "This is a tcltk example"
    0
    使用-nocase的话就不区分大小写,第一个例子:
    % string compare -nocase "This is a TCLTK example" "This is a tcltk example"
    0
    因为不区分大小写,所以两个字符串对比结果相等。
    只比较字符串的前7个字符"This is",再来使用第一个例子:
    % string compare -length 7 "This is a TCLTK example" "This is a tcltk example"
    0
    因为前面的7个字符是相同的,所以返回0。
 
    string equal ?-nocase? ?-length int? string1 string2
    这个命令比较两个字符串是否相同。相同返回1,不同返回0。
    以下的例子比较两个不同字符串:

    % string equal "This is a TCLTK example" "This is a tcltk example"
    -1
    比较两个相同的字符串:
    % string equal "This is a tcltk example" "This is a tcltk example"

    0
    -nocase与-length int的使用方法不再详述。
 
    string index string charIndex
    此命令的使用是string命令当中使用频率非常高的,而且也非常简单,就是取出字符串中索引为charIndex的字符。
    举例如下:
    % string index "This is a tcltk example" 14
    k
 
    string range string first last
    命令取出字符串中索引范围为first到last的字符(包括first和last,下同)。
    % string range "This is a tcltk example" 1 14
    his is a tcltk
 
    string first needleString haystackString ?startIndex?
    搜寻在haystackString字符串中与needleString字符串完全匹配的字符段,如果找到了返回第一次匹配在haystackString字符串中的索引,如果找不到则返回-1。如果指定了startIndex则在haystackString中从索引startIndex开始搜索。例如:
    % string first "tcltk" "This is a tcltk example"
    10
    返回的结果为字符串tcltk在字符串中第一次出现的子字符串第一个字符的索引。
    指定startIndex表示从这个索引开始搜索:
    % string first "is" "This is a tcltk example" 4
    5
    如果不指定索引4,则会搜索到This中的is
    % string first "is" "This is a tcltk example"
    2
 
    string last needleString haystackString ?lastIndex?
    与first命令相似,只是返回的是最后搜索到的匹配的字符段,lastIndex含义也有所不同,first命令中的index指定了从index往后搜索,而这里是从index往前,例如:
    % string last "is" "This is a tcltk example"
    5
    返回的is不是This中的is,可以指定lastIndex来表示从哪个索引开始搜索:
    % string last "is" "This is a tcltk example" 3
    2
    搜索到了This中的is
【上篇】
【下篇】

抱歉!评论已关闭.