Recently a discussion arose concerning a way of determining how many occurances of a character appeared in a string. Several methods were proposed: * [regexp] * [split] * [foreach]/[split]/[incr] * [string map] #! /usr/tcl84/bin/tclsh set mystring "line1\nline2\nline3\nline4\n" proc splitline {string} { set rc [llength [split $string "\n"]] incr rc -1 return $rc } proc regline {string} { set rc [regexp -line -all "\n" $string] return $rc } proc splitchar {string} { foreach x [split $string ""] {if {[catch {incr counters($x)}]} {set counters($x) 1}} set rc $counters(\n) return $rc } puts [time {splitline $mystring} 10000] puts [time {regline $mystring} 10000] puts [time {splitchar $mystring} 10000] On my SPARC Solaris, using Tcl 8.4, I get 49 microseconds per iteration 154 microseconds per iteration 963 microseconds per iteration as the result. - [RS]: Surprising, but good to know, that [split] is by far the fastest way... [Brett Schwarz] added this proc as well: proc strmap {str} { return [expr {[string length $str]-[string length [string map {"\n" ""} $str]]}] }; on my SuSE Linux 7.3 machine (2X333MHz PII) with Tcl 8.4, I get these results: 34 microseconds per iteration 87 microseconds per iteration 782 microseconds per iteration 34 microseconds per iteration So, it gives the same performance of the split version. However, if the string length is increased, to lets say 100 lineX's (i.e. 100 \n), then this proc using [string map] actually scales better. Here are the numbers with 100: 263 microseconds per iteration 1729 microseconds per iteration 2573 microseconds per iteration 141 microseconds per iteration There is quite an improvement....