I'm trying to figure out how many times one string occurs in another, which Tcl doesn't seem to have built-in. I can't use either of the solutions on the wiki, since I have to support Tcl 8.0.
This doesn't work:
# needleString is known to contain no regex metacharacters
llength [regexp -all -inline $needleString $haystackString]
because -all isn't supported on 8.0.
This doesn't work:
proc string_occurrences {needleString haystackString} {
set j [string first $needleString $haystackString 0]
if {$j == -1} {return 0}
set i 0
while {$j != -1 } {
set j [string first $needleString $haystackString [incr j]]
incr i
}
return $i
}
because string first doesn't support the startIndex argument on 8.0.
I could modify string_occurrences to use string range to take substrings of the string and 2-argument string first to search inside those, but that's even more cumbersome than the loop already is, and I don't know how efficient string range is. Do I have any better options?
You could use [regsub -all] (which is present in 8.0) to create a new string with the needleString removed, and compare lengths:
proc string_occurrences {needleString haystackString} {
regsub -all $needleString $haystackString {} stripped
expr {([string length $haystackString] - [string length $stripped]) / [string length $needleString]}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With