2013-07-29 43 views

回答

-1

您可以使用初始化函数来制作大写的第一个字母并以小写字母休息。

+3

我在交互式会话(Tcl 8.5.10)中执行'Initcap fooBar'并得到'无效的命令名称'initcap'',那么你在说什么? – kostix

+0

对不起,但我不熟悉Initcap函数。 – user2533429

3

string totitle命令关闭:除了第一个大写字符以外,它会将整个字符串降低。

set s {whAT is yOur hoUSe nUmBer ? Is iT 26.} 
string totitle $s 
What is your house number ? is it 26. 

要为每个字是多一点涉及:

proc CapitalizeEachWord {sentence} { 
    subst -nobackslashes -novariables [regsub -all {\S+} $sentence {[string totitle &]}] 
} 
set s {whAT is yOur hoUSe nUmBer ? Is iT 26.} 
CapitalizeEachWord $s 
What Is Your House Number ? Is It 26. 

的regsub命令接受的每个空间分离的词语和与该文字串替换它“[string totitle word]”:

"[string totitle whAT] [string totitle is] [string totitle yOur] [string totitle hoUSe] [string totitle nUmBer] [string totitle ?] [string totitle Is] [string totitle iT] [string totitle 26.]" 

我们使用subst命令来评估所有单个“字符串合计”命令。

+0

通常需要做更多的工作(为了避免输入字符串中的任何杂散Tcl元字符),但这是一个合理的方法。 –

相关问题