Shane’s script includes the ability to remove characters from the beginning, the end, or both the beginning and end of a string. I seldom need to do all of these in one script and, for economy of code, decided to divide Shane’s script into three handlers, which I’ve included below. I’ve also included a fourth handler which works on a string with multiple paragraphs.
The operation of these scripts requires no explanation, except to note that the handlers’ second parameter can either be specific characters or missing value, and, in the latter case, the character set whitespaceAndNewlineCharacterSet is used. So, when calling one of the handlers, all of the following are valid:
trimLeadingCharacters(theString, “-” & " ")
trimTrailingCharacters(theString, space & tab)
trimLeadingAndTrailingCharacters(theString, missing value)
I ran some timing tests with these handlers with single-paragraph strings and they generally took about 3 milliseconds to run–this assumes that the foundation framework is already in memory. I tested the fourth handler with a string that contained 301 paragraphs, and the timing result was 265 milliseconds.
TRIM LEADING CHARACTERS
on trimLeadingCharacters(theString, theCharacters)
if theCharacters = missing value then
set theCharacters to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
else
set theCharacters to current application's NSCharacterSet's characterSetWithCharactersInString:theCharacters
end if
set theString to current application's NSString's stringWithString:theString
set theRange to theString's rangeOfCharacterFromSet:(theCharacters's invertedSet())
if |length| of theRange = 0 then return ""
return (theString's substringFromIndex:(theRange's location)) as text
end trimLeadingCharacters
TRIM TRAILING CHARACTERS
on trimTrailingCharacters(theString, theCharacters)
if theCharacters = missing value then
set theCharacters to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
else
set theCharacters to current application's NSCharacterSet's characterSetWithCharactersInString:theCharacters
end if
set theString to current application's NSString's stringWithString:theString
set theRange to theString's rangeOfCharacterFromSet:(theCharacters's invertedSet()) options:(current application's NSBackwardsSearch)
if |length| of theRange = 0 then return ""
return (theString's substringToIndex:((theRange's location) + (theRange's |length|))) as text
end trimTrailingCharacters
TRIM LEADING AND TRAILING CHARACTERS
on trimLeadingAndTrailingCharacters(theString, theCharacters)
if theCharacters = missing value then
set theCharacters to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
else
set theCharacters to current application's NSCharacterSet's characterSetWithCharactersInString:theCharacters
end if
set theString to current application's NSString's stringWithString:theString
set theString to theString's stringByTrimmingCharactersInSet:theCharacters
return theString as text
end trimLeadingAndTrailingCharacters
TRIM LEADING AND TRAILING CHARACTERS - STRING HAS MULTIPLE PARAGRAPHS
on trimLeadingAndTrailingCharacters(theString, theCharacters)
if theCharacters = missing value then
set theCharacters to current application's NSCharacterSet's whitespaceCharacterSet()
else
set theCharacters to current application's NSCharacterSet's characterSetWithCharactersInString:theCharacters
end if
set theStrings to (current application's NSString's stringWithString:theString)'s componentsSeparatedByString:linefeed
set cleanedStrings to current application's NSMutableArray's new()
repeat with aString in theStrings
set aString to (aString's stringByTrimmingCharactersInSet:theCharacters)
(cleanedStrings's addObject:aString)
end repeat
return (cleanedStrings's componentsJoinedByString:linefeed) as text
end trimLeadingAndTrailingCharacters