目录
5.2 取字符:string index 和 string range
string index "hello world" 0
输出:h
string range "hello world" 6 end-1
输出:worl
- 索引从0开始
- end对应结尾
- 可以使用表达式,如
end-$i
5.3 长度、大小写转换、裁剪、重复
- 长度:
string length "hello world"
输出:11 - 大小写转换:
string toupper "Hello World"(小写:string tolower)
输出:HELLO WORLD - 裁剪
- 匹配两端:
string trim "abcxxxcba" ab
输出:cxxxc - 匹配左边:
string trimleft "abcxxxcba" ab
输出:cxxxcba - 匹配右边:
string trimright "abcxxxcba" ab
输出:abcxxxc
- 匹配两端:
- 重复:
string repeat "hello world" 2
输出:hello worldhello world
5.4 简单搜索
从左到右搜索字符串中第一个wo的首字符位置,找不到返回-1:
string first wo "hello worldhello world"
输出:6
指定从索引为10的字符开始搜索:
string first wo "hello worldhello world" 10
输出:17
string last: 从右到左搜索
5.5 字符串比较
string compare "hello" "Hello"
输出:1
string compare "Hello" "hello"
输出:-1
string compare "Hello" "Hello"
输出:0
忽略大小写:string compare -nocase "Hello" "hello"
输出:0
设置比较的长度:string compare -length 1 "hELLO" "hello"
输出:0
只返回0(不相同)或1(相同):string equal -nocase -length 2 "hello" "Hexxx"
输出:1
5.6 字符串置换
string replace
string replace "hello world" 6 10 "Tcl"
输出:hello Tcl- 参数
6 10被替换字符串索引的起始与结束。 - 参数
Tcl:可以省略,结果等效为删除。
- 参数
string map dictionary string
将string中出现的的所有dictionary关键字置换为相应的值。set entities { action1 hello action2 learn name Tcl } string map -nocase $entities {Action1 name,action2 name}</

本文介绍Tcl语言中字符串处理的方法,包括字符取值、长度计算、大小写转换、字符串搜索与比较、格式化创建字符串、正则表达式匹配与替换等功能,并详细解释了各种命令的使用方法。
最低0.47元/天 解锁文章
1998

被折叠的 条评论
为什么被折叠?



