Re: getting chunks of text
Re: getting chunks of text
- Subject: Re: getting chunks of text
- From: Andrew Oliver <email@hidden>
- Date: Thu, 17 Mar 2005 01:44:49 -0800
On 3/16/05 7:40 PM, "Scott Haneda" <email@hidden> wrote:
> Given a string of length unknown, I need to chop it up to less than 256
> characters, however, I do not want to chop a word in half, so scan backwards
> to the first space before 256 chars. Not to worried about what would happen
> when running into punctuation and such.
>
> I need a result set that I can repeat through, thanks.
Here's one approach.
It works by progressing through the text items of the source text until word
n+1 trips the 256 character limit at which point it saves state and starts a
new 'chunk'.
It's not fast, and there are several optimization techniques I can think of
trying, but if you're dealing with SMS messages I'm guessing you not going
to be processing novels through this thing.
Andrew
:)
set theText to "your text here"
set {oldDelims, my text item delimiters} to {my text item delimiters, " "}
set _words to text items of theText
set chunks to {}
set currentChunk to ""
repeat with eachword in _words
if (length of (currentChunk & eachword)) > 256 then
copy currentChunk to end of chunks
set currentChunk to eachword
else
set currentChunk to currentChunk & " " & eachword
end if
end repeat
if currentChunk "" then copy currentChunk to end of chunks
set my text item delimiters to oldDelims
return chunks
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden