我有字符串例如:
Test Test Test Something Something
我想计算这个字符串重复的次数。
结果将是:
Test 3 Something 2
任何人都可以有想法,怎么做?
答案 0 :(得分:1)
proc countwords {str} {
foreach word [split $str] {incr count($word)}
foreach word [array names count] {puts "$word $count($word)"}
}
set string {Test
Test
Test
Something
Something}
countwords $string
Test 3
Something 2
答案 1 :(得分:1)
要知道特定字在字符串中重复的次数:
regexp -all {\yTest\y} $theString
要计算所有字词:
foreach word [regexp -all -inline {\w+} $theString] {
incr histogram($word)
}
parray histogram