如何用数字分割字符串

时间:2017-02-26 13:34:53

标签: tcl

我想分手但仍然失败。

set strdata "34a64323R6662w0332665323020346t534r66662v43037333444533053534a64323R6662w0332665323020346t534r66662v430373334445330535"

puts [split $strdata "3334445330535"] ;#<---- this command does not work

The result needed as below:
{34a64323R6662w0332665323020346t534r66662v43037} {34a64323R6662w0332665323020346t534r66662v43037}

2 个答案:

答案 0 :(得分:0)

split命令不起作用,请参阅文档。

尝试将数据字符串放入如下列表中:

regsub -all 3334445330535 $strdata " "

即。用空格替换分隔符。

文档: regsubsplit

答案 1 :(得分:0)

split命令的可选第二个参数被解释为要分割的 set 字符,所以它实际上并不是你想要的。但是,还有其他方法。做你想做的更简单的方法之一是使用string map将字符序列转换为输入数据中不包含的字符(Unicode充满了那些!)然后{{1}在那:

split

请注意,最后有一个set strdata "34a64323R6662w0332665323020346t534r66662v43037333444533053534a64323R6662w0332665323020346t534r66662v430373334445330535" set splitterm "3334445330535" set items [split [string map [list $splitterm "\uFFFF"] $strdata] "\uFFFF"] foreach i $items { puts "==> $i" } # ==> 34a64323R6662w0332665323020346t534r66662v43037 # ==> 34a64323R6662w0332665323020346t534r66662v43037 # ==> {} (即一个空字符串列表元素),因为它是在最后一个split元素之后的字符串。如果您不想要,请在{}string trimright之间添加string map

split