Re: Trim?
Re: Trim?
- Subject: Re: Trim?
- From: Paul Skinner <email@hidden>
- Date: Sat, 3 Aug 2002 12:09:57 -0400
On Saturday, August 3, 2002, at 09:24 AM, Nigel Garvey wrote:
John Delacour wrote on Sat, 3 Aug 2002 02:35:46 +0100:
At 2:37 pm -0700 2/8/02, email@hidden wrote:
Does anyone know if there's a simple way to trim white space off of a
string, or should I just loop through it and take them out myself?
set s to " This is > a string "
set AppleScript's text item delimiters to {space}
set s to "" & words of s
set AppleScript's text item delimiters to {""}
return s
Or, of course:
set s to " This is > a string "
return text from word 1 to word -1 of s
My version doesn't tidy up multiple spaces between words, whereas John's
does. However, his might also insert spaces where none were previously -
eg. around the "@" in an e-mail address. Both scripts, unfortunately,
zap
punctuation as well as white space.
The following effort covers all the eventualities, as far as I can see:
set s to " email@hidden 'This is a > string?' "
set AppleScript's text item delimiters to {space}
set l to s's text items
set AppleScript's text item delimiters to {""}
-- Prepend a space to every non-blank text item except the first
set started to false
repeat with thisItem in l
if (count thisItem) > 0 then
if started then
set thisItem's contents to (space & thisItem)
else
set started to true
end if
end if
end repeat
return l as string --> "email@hidden 'This is a > string?'"
NG
You could skip some of the looping.
set inputText to " email@hidden 'This is a > string?' "
cleanSpaces(inputText)
-->"email@hidden 'This is a > string?'"
on cleanSpaces(inputText)
set tag to "o#?"
set inputText to tag & inputText & tag
repeat while inputText contains " "
repeat with thesedelimiters in {{space, tag}, {tag & tag, tag}, {tag,
space}}
set AppleScript's text item delimiters to item 1 of thesedelimiters
set tempS to text items of inputText
set AppleScript's text item delimiters to item 2 of thesedelimiters
set inputText to tempS as text
end repeat
end repeat
set AppleScript's text item delimiters to ""
return (characters 2 thru -2 of inputText) as text
end cleanSpaces
--
Paul Skinner
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.
References: | |
| >Re: Trim? (From: Nigel Garvey <email@hidden>) |