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

本文详细介绍了TCL/Tk中的string命令,包括repeat、replace、reverse、tolower、totitle、toupper、trim、trimleft、trimright、wordend和wordstart等子命令的使用实例,帮助读者更好地理解和应用这些功能。
    这里对string命令中的几个子命令使用实例进行一些解释,以便于更加容易理解string命令中的各个子命令,本文仅对以下几个string命令进行实例解析。分别是repeat、replace、reverse、tolower、totitle、toupper、trim、trimleft、trimright、wordend和wordstart几个子命令。
 
    string repeat string count
    非常简单,返回一个把string重复count次的字符串。
    % string repeat "This" 3
    ThisThisThis
 
    string replace string first last ?newstring?
    也很简单,使用newstring替换string中的first到last的字符串,如果没有newstring,就是使用空代替。
    % string replace "This is a tcltk example" 10 14 TCLTK
    This is a TCLTK example
    如果没有newstring:
    % string replace "This is a tcltk example" 10 14
    This is a  example
 
    string reverse string
    返回string的反序字符串:
    % string reverse "This is a tcltk example"
    elpmaxe ktlct a si sihT
 
    string tolower string ?first? ?last?
    string totitle string ?first? ?last?
    string toupper string ?first? ?last?
    这三个命令放在一起,是因为三个命令的格式完全相同,只是实现了不同的操作。
    将一个字符串全部变为小写形式:
    % string tolower "This is a tcltk example"
    this is a tcltk example
    将一个字符串全部变为大写形式:
    % string toupper "This is a tcltk example"
    THIS IS A TCLTK EXAMPLE
    将一个字符串里面开头的第一个字母转换为大写形式,其他字符转化为小写形式:
    % string totitle "this is a TCLTK example"
    This is a tcltk example
    first和last指定了转换的范围,操作与上述完全相同,只是对字符串的作用范围不同。
 
    string trim string ?chars?
    string trimleft string ?chars?
    string trimright string ?chars?
    这三个命令实现的功能类似,都是去掉chars字符,只是操作的位置有所区别。如果没有指定chars字符,则去掉空白符(包括空格符、制表符、换行符、回车符)。trim对字符串开头和结尾都操作,trimleft只对字符串开头操作,trimright只对字符串结尾操作。
    % string trim "!!This is a tcltk example!!" !
    This is a tcltk example
    % string trimleft "!!This is a tcltk example!!" !
    This is a tcltk example!!
    % string trimright "!!This is a tcltk example!!" !
    !!This is a tcltk example
 
    string wordend string charIndex
    string wordstart string charIndex
    这两个命令类似,wordend是找出给定索引的字符所在的单词的下一个单词的第一个字符的索引,wordstart是找出给定索引的字符所在的单词的第一个字符的索引。用语言描述比较难理解,下面举例说明就非常清晰了:
    % string wordend "This is a tcltk example" 12
    15
    12索引为tcltk中的l字符,那么返回的结果就是l所在的词tcltk的下一个词example中的第一个字符e的索引,即15。
    % string wordstart "This is a tcltk example" 12
    10
    12索引为tcltk中的l字符,那么返回的结果就是l所在的词的第一个字符t的索引,即10。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值