1

我有一段代码用于我拥有的程序[因此一些特定于应用程序的代码] ...无论如何,我试图将每个单词的第一个字母大写,除非该单词大写。

例如: >>这是将要更改的文本。

到目前为止,我的代码如下。

同样,其中一些是特定于应用程序的,我无法使用“puts”,结果必须作为 return“”返回,这就是我创建一个 var 并逐字添加的原因。

proc ToTitle {} {
set Input [sh_set clipboard]
set CleanedInput [string map {" " |} [string trimright [string trimleft $Input]]]
set InputList [split $CleanedInput "|"]
set wresult ""
set item 0
foreach line $InputList {
set List_Item [lindex $InputList $item];
if {[string is upper $List_Item] == 1} {
 set newline $List_Item
    set wresult "$wresult $newline"
    incr item   
} else {
 set newline [string totitle $List_Item]
    set wresult "$wresult $newline"
    incr item
}
}
regsub -all {\u0020{2,}} $wresult " " wresult; #REMOVE ALL EXCESSIVE SPACE CHARACTERS
set $wresult [string trimright [string trimleft $wresult]]; # TRIM ALL OF THE WHITESPACE BEFORE AND AFTER THE STRING
return "$wresult"}

这目前正在工作,输出将是: 这是将要更改的文本。

问题是“改变”。因为“。” 问题是我可以使用什么来仅读取具有特殊字符或单词字符的项目上的单词字符?

{[string is upper $List_Item] == 1}

我知道我可以添加一些东西来检查它......提前感谢您的所有帮助。

4

1 回答 1

4

我认为有一个更简单的解决方案。试试这个:

set a "this is text THAT would be CHANGED."
set out ""
foreach word $a {
append out "[string toupper $word 0 0] "
}
puts $out

运行它会给出以下输出:

% % % This Is Text THAT Would Be CHANGED.
于 2013-01-07T00:42:07.107 回答