Count occurrences of a substring: Difference between revisions

Content added Content deleted
(→‎{{header|AppleScript}}: Added the simple vanilla method.)
Line 213: Line 213:
{3, 2}
{3, 2}
</pre>
</pre>
----
The above assertions notwithstanding, it's always been possible to use AppleScript's text item delimiters for this purpose with its native strings, except that the TIDs have only observed the current considering/ignoring state with the 'unicode text' class, which was introduced around Mac OS X 10.4 and became AppleScript's only native text class with the introduction of AppleScript 2.0 in Mac OS X 10.5.

<lang applescript>on countSubstring(theString, theSubstring)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to theSubstring
set substringCount to (count theString's text items) - 1
set AppleScript's text item delimiters to astid
return substringCount
end countSubstring

{countSubstring("the three truths", "th"), countSubstring("ababababab", "abab")}</lang>

{{Out}}
<lang applescript>{3, 2}</lang>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==