Adrian Nier Code

Back to overview

Pattern count

Last modification: Monday, December 08, 2008 02:40 pm

Counts the occurrences of a string in another.

Required parameters
Parameter 1 (string)
A string that is matched with the pattern.

Parameter 2 (string)
A string that serves as a pattern.

Implementation

on patternCount(_string, _pattern)
   
   if _string does not contain _pattern then return 0
   
   set _prvDlmt to AppleScript's text item delimiters
   try
      set AppleScript's text item delimiters to _pattern
      set _patternCount to (count of text items of _string) - 1
   on error
      set _patternCount to 0
   end try
   set AppleScript's text item delimiters to _prvDlmt
   
   return _patternCount
   
end patternCount