• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: cutting multiple sections from a string?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cutting multiple sections from a string?


  • Subject: Re: cutting multiple sections from a string?
  • From: Nigel Garvey <email@hidden>
  • Date: Tue, 03 Sep 2013 10:52:53 +0100

Shane Stanley wrote on Tue, 03 Sep 2013 15:49:14 +1000:

>On 03/09/2013, at 2:33 PM, Alex Hall <email@hidden> wrote:
>
>> I need a way to remove chunks from a string given the string and the
sets
>>of numbers, like {{1,3}, {8,16}}.
>
>on removeChunks_inString_(rangeList, theString)
>	set rangeList to reverse of rangeList
>	repeat with aRange in rangeList
>		if item 1 of aRange = 1 then
>			set theString to text ((item 2 of aRange) + 1) thru -1 of theString
>		else if item 2 of aRange = (length of theString) then
>			set theString to text 1 thru ((item 1 of aRange) - 1) of theString
>		else
>			set theString to text 1 thru ((item 1 of aRange) - 1) of theString &
>text ((item 2 of aRange) + 1) thru -1 of theString
>		end if
>	end repeat
>end removeChunks_inString_
>
>removeChunks_inString_({{1, 3}, {8, 16}}, "I need a way to remove chunks
>from a string given the string and the sets of numbers")
>
>It needs error checking, basically that item 2 of aRange is no greater
than
>the string's length.

Also that the ranges are in order and don't overlap.

Assuming that they are and don't, here's a less brain-taxing effort:

  on removeChunks_inString_(rangeList, theString)
    set out to ""
    set de to 1
    repeat with aRange in rangeList
      set ad to (beginning of aRange) - 1
      if (ad > 0) then set out to out & text de thru ad of theString
      set de to (end of aRange) + 1
    end repeat
    if (de ≤ (count theString)) then set out to out & text de thru -1 of theString
    return out
  end removeChunks_inString_

  removeChunks_inString_({{1, 3}, {8, 16}}, "I need a way to remove chunks from a string given the string and the sets of numbers")


NG

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden


  • Follow-Ups:
    • Re: cutting multiple sections from a string?
      • From: Alex Hall <email@hidden>
  • Prev by Date: Re: cutting multiple sections from a string?
  • Next by Date: Re: cutting multiple sections from a string?
  • Previous by thread: Re: cutting multiple sections from a string?
  • Next by thread: Re: cutting multiple sections from a string?
  • Index(es):
    • Date
    • Thread