Adrian Nier Code

Back to overview

Search and replace

Last modification: Monday, August 25, 2008 12:47 am

Replaces every occurrence of a string and replaces it with another. Takes three strings (whole string, search string and replacement string) as parameters.

Required parameters
Parameter 1 (string)
The string to be searched.

Parameter 2 (string)
The string to search with.

Parameter 3 (string)
The replacement string.

Implementation

on searchAndReplace(_string, _search, _replace)
   
   if _string does not contain _search then return _string
   
   set _prvDlmt to AppleScript's text item delimiters
   try
      set AppleScript's text item delimiters to _search
      set _stringItems to text items of _string
      set AppleScript's text item delimiters to _replace
      set _string to _stringItems as string
   end try
   set AppleScript's text item delimiters to _prvDlmt
   
   return _string
   
end searchAndReplace