如何删除字符" {"和"}"使用TCL从文件中读取后从字符串?

时间:2015-07-20 21:17:25

标签: string tcl

字符串:

{ii= 10 } & {jj= 2 }

我需要:

ii= 10 & jj= 2

我试过了: string trimstring replacelreplace, 他们不适合我。

2 个答案:

答案 0 :(得分:1)

如果你有

set x "{ii= 10 } & {jj= 2 }"

你可以做到

string map {\{ {} \} {}} $x
# -> ii= 10  & jj= 2 

即。用空字符串替换文字的左右括号。

但是你的字符串实际上看起来很像列表的字符串表示,自动插入大括号来保留空格。使用它作为列表,您需要做的就是

join $x
# -> ii= 10  & jj= 2 

使用string trim不起作用,因为它只会删除字符串末尾的字符:

string trim $x \{\}
# -> ii= 10 } & {jj= 2 

在这种情况下,string replace命令太多了。

lreplace命令适用于列表元素,而不适用于字符串中的字符。

文档:joinlreplacesetstring

答案 1 :(得分:0)

这应该有效:

% set x "{ii= 10 } & {jj= 2 }"
{ii= 10 } & {jj= 2 }
% regsub -all {\{|\}} $x ""
ii= 10  & jj= 2

在这里,它只删除所有花括号并替换为空字符串 - 就像删除它们一样