Adrian Nier Code

Back to overview

Strip whitespace

Last modification: Thursday, June 11, 2009 03:08 pm

Strips the leading and trailing whitespace of a string passed to it.

Required parameters
Parameter 1 (string)
A string that is to be stripped of leading and trailing whitespace

Implementation

on stripWhitespace(_string)
   
   set _charCount to count of characters of _string
   
   -- Exit early if there's no string to process
   if _charCount is 0 then return ""
   
   -- Determine the first character that is not whitespace
   repeat with _startChar from 1 to _charCount
      if (ASCII number (character _startChar of _string)) > 32 then exit repeat
   end repeat
   
   -- Determine the last characters that is not whitespace
   set _endChar to _charCount
   repeat
      if (ASCII number (character _endChar of _string)) > 32 then exit repeat
      set _endChar to _endChar - 1
      if _endChar is 0 then exit repeat
   end repeat
   
   -- Get the substring
   set _prvDlmt to AppleScript's text item delimiters
   set AppleScript's text item delimiters to ""
   set _string to characters _startChar thru _endChar of _string as string
   set AppleScript's text item delimiters to _prvDlmt
   
   return _string
   
end stripWhitespace