Tcl学习笔记1——string list

string

string cat ?string1? ?string2...? # 连接
set s1 "hello"
set s2 "world"
string cat $s1 $s2
hello world

string compare ?-nocase? ?-length length? string1 string2 # 对比;-nocase忽略字母大小写;-length对比前length个字符
string compare $s1 $s2
0

string equal ?-nocase? ?-length length? string1 string2 # 等于
string equal $s1 $s2
1

string first needleString haystackString ?startIndex? #查找;last
string first "123" "abc123abc123"
3
string index string charIndex # 索引
string index $s2 2
r

string is class ?-strict? ?-failindex varname? string  # 判断
# class {alnum alpha ascii boolean control digit double entier false graph integer list lower print punct space true upper wideinteger wordchar xdigit}
string is alpha "ab2"
0

string length string # 字符串长度
string length $s1
5

string range string first last # 取范围
string range $s1 0 1
he

string repeat string count # 重复
string repeat # 3
###

string replace string first last ?newstring? # 替换
string replace "hello" 1 2 ""
hlo

string reverse string # 反转
string tolower string ?first? ?last? #大小写转换
string totitle string ?first? ?last?
string toupper string ?first? ?last?
string trim string ?chars? # 去除两侧空字符串
string trimleft string ?chars?
string trimright string ?chars?
string map ?-nocase? mapping string # 对应
string match ?-nocase? pattern string # 匹配
string bytelength string
string wordend string charIndex
string wordstart string charIndex

list

set lst1 [list 1 2 3 4 5] #!!!下列结果均默认 set lst1 [list 1 2 3 4 5]
llength $lst1
5
lindex $lst1 end-1
4
lrange $lst1 0 2
1 2 3
linsert $lst1 1 9
1 9 2 3 4 5
lset lst1 1 ""
1 {} 3 4 5
lreplace $lst1 1 1 ""
1 {} 3 4 5
lreplace $lst1 1 1
1 3 4 5
concat $lst1 $lst2
1 2 3 4 5 a b c
list $lst1 $lst2
{1 2 3 4 5} {a b c}
lappend lst1 6
1 2 3 4 5 6
lsearch $lst 3
2
lappend lst 3
lsearch -all $lst 3
2 5
lsearch -all -inline $lst 3
3 3
#补充1 lsearch -regexp -not -nocase -start charIndex -subindices -integer
lsort [list 1 3 2 4 5]
1 2 3 4 5 
lsort [list 1 3 2 04 5]
04 1 2 3 5
lsort -integer [list 1 3 2 04 5]
1 2 3 04 5
#补充2 lsort -index charIndex -decreasing -uniq

string和list很多命令是类似的,亦可通过join相互转换

set lst3 [split "hello" ""]
h e l l o
set newstr [join $lst3 ""]
hello
set newstr [join $lst3 "-"]
h-e-l-l-o

参考 ActiveTcl 8.6.9 Documentation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值