TCL中字符串的替換
在TCL中可以使用 stringreplace命令進行字符串替換。它接受一個字符串作為參數,以及要删除的字符序列的開頭和結尾索引,還可選地接受一個字符串參數作為替換用字符串。例如:
string replace "San Diego,California" 4 8 "Francisco"
san Francisco,California
string replace "parsley, sage,rosemary, and thyme" 0 8
sage, rosemary, and thyme
string map命令根據字典把字符串中的相應文本替換為對應的值。這可以用于模闆功能。其基本語法如下
string map dictionary string
string map命令将string中出現的所有dictionary關鍵字置換為相應的值,返回置換後的字符串。置換是按順序進行的,列表中先出現的關鍵字先處理。隻對字符串疊代一次,所以前面進行的置換不會影響接下來的匹配查找,例如:
set entities {
& &
' '
> >
< <
\" "
}
string map $entities {if (index>0 &&nbAtts==0)}
if (index > 0 && nbAtts==0)
使用-nocase選項,就不區分關鍵字的大小寫,例如:
string map –nocase \
{RESOURCE "Ms. Ripley"CORPORATION "Weyland-Yutani"} \
"Dear ResouRcE, welcome to your firstday at corporation"
Dear Ms. Ripley, welcome to your first dayat Weyland-Yutani
在TCL中通過正則表達式還可以進行更複雜的替換操作。