Chinaunix首页 | 论坛 | 博客
  • 博客访问: 182564
  • 博文数量: 42
  • 博客积分: 2235
  • 博客等级: 大尉
  • 技术积分: 460
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-15 20:17
文章分类

全部博文(42)

文章存档

2012年(1)

2011年(7)

2010年(33)

2009年(1)

我的朋友

分类:

2010-02-06 11:28:28

string ----操作字符串

语法:
string option arg ?arg ...?

根据option执行一系列的string操作合法的option如下:

string bytelength string
  返回在内存中用来表示string的字节数。由于UTF-8使用1到3个字节来表示Unicode字符,一般情况下字节长度将和字符长度不同。脚本很少关注字节长度。在大多数情况下,都是用string length。可以参考Tcl_NumUtfChars手册来获得UTF-8的细节。

string compare ?-nocase? ?-length int? string1 string2
  使用字符接字符方式来比较字符串string1和string2。根据字典排序的小于/等于/大于可以返回-1,0或者1。如果-length被指定,此时只有前length个字符进行比较。如果-length为负数,则被忽略。如果-nocase被指定,以大小写字母相等通用方式比较

string equal ?-nocase? ?-length int? string1 string2
  使用字符接字符方式来比较字符串string1和string2。如果string1和string2相同则返回1,不同返回0。如果-length被指定,只有前length个字符进行匹配。如果-length为负,则其被忽略。如果指定-nocase,则比较时忽略大小写

string first string1 string2 ?startIndex?
  在string2中按字符顺序精确查找string1。如果找到,返回首次匹配的第一个字符的索引,没找到则返回-1。如果startIndex被指定,则表示自startIndex开始进行搜索,如:
string first a 0a23456789abcdef 5

will return 10, but

string first a 0123456789abcdef 11

will return -1.

string index string charIndex
  按照charIndex序号返回string中的字符。charIndex为0表示string的第一个字符。charIndex可以指定如下:
  integer 整形数
  end    string的最后一个字符
  end-integer 倒数第几个字符,比如,end-1表示在"abcd"中的"c"
  如果charIndex少于0或者大于等于string的长度时空字符串返回

string is class ?-strict? ?-failindex varname? string
  如果string为指定字符形式之一则返回1,其他返回0。如果指定-strict,对于空字符串返回0,其他返回空字符串对于任意形式返回1。如果指定-failindex,如果函数返回0,则当class不再有效时的字符串的索引将被存储在varname。如果函数返回为1,varname不会被赋值。class有以下数据:
alnum
    Any Unicode alphabet or digit character.

alpha
    Any Unicode alphabet character.

ascii
    Any character with a value less than \u0080 (those that are in the 7-bit ascii range).

boolean
    Any of the forms allowed to Tcl_GetBoolean.

control
    Any Unicode control character.

digit
    Any Unicode digit character. Note that this includes characters outside of the [0-9] range.

double
    Any of the valid forms for a double in Tcl, with optional surrounding whitespace. In case of under/overflow in the value, 0 is returned and the varname will contain -1.

false
    Any of the forms allowed to Tcl_GetBoolean where the value is false.

graph
    Any Unicode printing character, except space.

integer
    Any of the valid forms for an ordinary integer in Tcl, with optional surrounding whitespace. In case of under/overflow in the value, 0 is returned and the varname will contain -1.

lower
    Any Unicode lower case alphabet character.

print
    Any Unicode printing character, including space.

punct
    Any Unicode punctuation character.

space
    Any Unicode space character.

true
    Any of the forms allowed to Tcl_GetBoolean where the value is true.

upper
    Any upper case alphabet character in the Unicode character set.

wordchar
    Any Unicode word character. That is any alphanumeric character, and any Unicode connector punctuation characters (e.g. underscore).

xdigit
    Any hexadecimal digit character ([0-9A-Fa-f]).
    
string last string1 string2 ?lastIndex?
  在string2中查找精确匹配的string1。如果找到,则返回最后一次匹配string1的第一个字符的index。如果没有匹配,返回-1。如果指定lastIndex,则只有在lastIndex序号之前进行匹配操作,比如:

string last a 0a23456789abcdef 15

will return 10, but

string last a 0a23456789abcdef 9

will return 1.

string length string
  返回string的十进制长度值。注意并不是必须以存储string的字节数完全相同。如果对象为一个ByteArray对象,则一般将返回对象的字节长度。
 
string map ?-nocase? mapping string
  通过mapping规则对string进行转换,存在急于求功的原则,即先匹配先替换

string map {abc 1 ab 2 a 3 1 0} 1abcaababcabababc

will return the string 01321221.

string map {1 0 ab 2 a 3 abc 1} 1abcaababcabababc

it will return the string 02c322c222c.

string match ?-nocase? pattern string
  查看pattern是否匹配string,匹配返回1,不匹配返回0。如果指定-nocase,则忽略大小写区别。也可以用到特殊字符:
    *
        Matches any sequence of characters in string, including a null string.

    ?
        Matches any single character in string.

    [chars]
        Matches any character in the set given by chars. If a sequence of the form x-y appears in chars, then any character between x and y, inclusive, will match. When used with -nocase, the end points of the range are converted to lower case first. Whereas {[A-z]} matches '_' when matching case-sensitively ('_' falls between the 'Z' and 'a'), with -nocase this is considered like {[A-Za-z]} (and probably what was meant in the first place).

    \x
        Matches the single character x. This provides a way of avoiding the special interpretation of the characters *?[]\ in pattern.

string range string first last
  根据first和last的索引从string返回一系列连续的字符串,索引0指string的第一个字符。first和last越界则会通过正常方式进行处理。

string repeat string count
  按count次数返回重复字符串

string replace string first last ?newstring?
  按照first和last的索引将newstring替换string的first和last范围的字符

string tolower string ?first? ?last?
  按照first和last的索引将string进行小写转换

string totitle string ?first? ?last?
  按照first和last的索引将string的first索引的字符转换为大写

string toupper string ?first? ?last?
  按照first和last的索引将string的first到last的字符转换为大写
 
string trim string ?chars?
  从string的两端截除匹配chars的任何字符

string trimleft string ?chars?
  从string的左端截除匹配chars的任何字符

string trimright string ?chars?
  从string的右端截除匹配chars的任何字符


string wordend string charIndex
  wordend是找出给定索引的字符所在的单词的下一个单词的第一个字符的索引
string wordend "This is a tcltk example" 12
15
12索引为tcltk中的l字符,那么返回的结果就是l所在的词tcltk的下一个词example中的第一个字符e的索引,即15。   

string wordstart string charIndex
  wordstart是找出给定索引的字符所在的单词的第一个字符的索引
string wordstart "This is a tcltk example" 12
10
12索引为tcltk中的l字符,那么返回的结果就是l所在的词的第一个字符t的索引,即10。



set length [string length $string]
if {$length == 0} {
   set isPrefix 0
} else {
   set isPrefix [string equal -length $length $string "foobar"]
}
  
阅读(2343) | 评论(0) | 转发(0) |
0

上一篇:分支循环控制命令

下一篇:concat命令

给主人留下些什么吧!~~