Re: Remove extra spaces from a string
Re: Remove extra spaces from a string
- Subject: Re: Remove extra spaces from a string
- From: Christopher Nebel <email@hidden>
- Date: Mon, 9 Jun 2003 14:58:21 -0700
On Monday, June 9, 2003, at 02:09 PM, Emmanuel wrote:
set withspaces to "Extra spaces."
display dialog "Before: " & withspaces
set withoutspaces to do shell script ("echo " & withspaces & " | tr
-s ' '")
display dialog "After: " & withoutspaces
Since we mention "tr" we have to mention also that it works only on
strings up to 64KB.
Actually, tr(1) can handle any amount of data. The shell, however, can
only pass about 64KB worth of arguments around, hence the limit.
Amusingly, this example is written in such a way that the "tr" is
unnecessary. Because the OP didn't quote the "argument" to echo(1),
the shell sees each whitespace-delimited thing as a different argument,
and echo puts exactly one space between each of its arguments. Of
course, this script would also fail if "withspaces" contained any
quotes. A more correct version would be this:
set withspaces to "Extra spaces."
display dialog "Before: " & withspaces
set withoutspaces to do shell script ("echo " & quoted form of
withspaces & " | tr -s ' '")
display dialog "After: " & withoutspaces
--Chris Nebel
Apple Development Tools
_______________________________________________
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.