当前位置:文档之家› TCL基础教程——(4)字符串处理

TCL基础教程——(4)字符串处理

TCL基础教程——(4)字符串处理
TCL基础教程——(4)字符串处理

TCL基础教程——(4)字符串处理

对于任何一种脚本语言来说,强大的字符串处理功能都是为人们所津津乐道的,TCL也不例外,那么究竟TCL的字符串处理有什么功能呢?下面将介绍简单模式匹配,在日后的文章中,还将介绍正则表达式。String命令

String命令实际上是一组操作字符串的命令,它的第一个变元决定了进行什么样子的操作,所有String 的命令如下:

对于我来说,常用的有如下几个方法,length,equal,match,range,first。请看下面的程序[ppcorn@localhost ppcorn]$ cat strtest.tcl #!/usr/bin/tclsh

set str1 str1

set str2 str1

set str3 isstr1?

set str4 he index of str1

# print the length of str1,the value should be 4

puts [string length $str1]

# print the str1 equal str2, the value should be 1

puts [string equal $str1 $str2]

# print the str2 match str3, the value should be 1

puts [string match *$str1* $str3]

# print the 4 to 9 of str4, the value should be index

puts [string range $str4 4 9]

# print the postion of first str1 in str4, the value should be 13

puts [string first $str1 $str4]

[ppcorn@localhost ppcorn]$ ./strtest.tcl

4

1

1

index

13

请注意一下string match的用法,中间使用了*$str1*的用法,这里使用了模糊匹配。一共有三种进行匹配的方式

具体用法如

string match ?? XY

string match {[ab]*x} box

string match {[a-zA-Z0-9]} $char

其中$char为任意字符

等等,上述值都为1,表示匹配,有兴趣的朋友可以自己调试。

相关主题
文本预览
相关文档 最新文档