Count occurrences of a substring: Difference between revisions

Content added Content deleted
(Added AutoHotkey example)
m (alphabetized)
Line 11: Line 11:
The matching should yield the highest number of non-overlapping matches. In general, this essentially means matching from left-to-right or right-to-left (see proof on talk page).
The matching should yield the highest number of non-overlapping matches. In general, this essentially means matching from left-to-right or right-to-left (see proof on talk page).


=={{header|AutoHotkey}}==
While it is simple enough to parse the string, AutoHotkey has a rather unconventional method which outperforms this. StringReplace sets the number of replaced strings to ErrorLevel.
<lang AutoHotkey>MsgBox % countSubstring("the three truths","th") ; 3
MsgBox % countSubstring("ababababab","abab") ; 2

CountSubstring(fullstring, substring){
StringReplace, junk, fullstring, %substring%, , UseErrorLevel
return errorlevel
}</lang>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Line 47: Line 38:
3 2 2
3 2 2
</pre>
</pre>

=={{header|AutoHotkey}}==
While it is simple enough to parse the string, AutoHotkey has a rather unconventional method which outperforms this. StringReplace sets the number of replaced strings to ErrorLevel.
<lang AutoHotkey>MsgBox % countSubstring("the three truths","th") ; 3
MsgBox % countSubstring("ababababab","abab") ; 2

CountSubstring(fullstring, substring){
StringReplace, junk, fullstring, %substring%, , UseErrorLevel
return errorlevel
}</lang>


=={{header|C}}==
=={{header|C}}==