Re: getting chunks of text
Re: getting chunks of text
- Subject: Re: getting chunks of text
- From: Nigel Garvey <email@hidden>
- Date: Thu, 17 Mar 2005 20:49:21 +0000
Scott Haneda wrote on Wed, 16 Mar 2005 19:40:49 -0800:
>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.
I'd go for the two-pointer approach:
set theText to "Mae seryddwyr yn dweud eu bod wedi canfod beth maent yn
credu yw galaeth anweledig newydd. Mae'r tîm, sy'n cael eu harwain gan
Brifysgol Caerdydd, yn honni mai dyma'r cyntaf i'w chanfod sydd wedi ei
wneud o fater tywyll. Ardal o fewn y bydysawd yn cynnwys cyfanswm mawr o
fás sy'n troi fel galaeth ond nad yw'n cynnwys sêr yw galaeth dywyll.
Cafodd ei darganfod 50 miliwn o flynyddoedd goleuni i ffwrdd drwy
ddefnyddio telesgopau radio yn Sir Gaer a Puerto Rico."
set textLength to (count theText)
set maxLength to 256
set whiteSpace to space & return & ascii character 10
set collector to {}
set l to 1
considering case
repeat while character textLength of theText is in whiteSpace
set textLength to textlength - 1
end repeat
repeat until l > textLength
set r to l + maxLength - 1
if r < textLength then
repeat until character r of theText is in whiteSpace
set r to r - 1
end repeat
else
set r to textLength
end if
set end of collector to text l thru r of theText
set l to r + 1
end repeat
end considering
collector --> list of shorter strings
NG
_______________________________________________
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