• 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: Now, I have “Left(StringSource, CharCount)” and “Right(StringSource, CharCount)”
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Now, I have “Left(StringSource, CharCount)” and “Right(StringSource, CharCount)”


  • Subject: Re: Now, I have “Left(StringSource, CharCount)” and “Right(StringSource, CharCount)”
  • From: Christopher Nebel <email@hidden>
  • Date: Mon, 27 Oct 2008 18:47:23 -0700

On Oct 26, 2008, at 7:40 AM, Emile SCHWARZ wrote:

Here are the two other functions to work with string (that I missed, more or less):

display dialog my StringLeft("Salut les filles.", 5) -- Display: “Salut”
display dialog my StringRight("Salut les filles.", 7) -- Display: “filles.”


-- ********** ********** ********** ********** **********
--
-- “StringLeft(StringSource, CharCount)” handler definition
on StringLeft(StringSource, CharCount)
-- StringSource: the string you want to extract characters from
-- CharCount: the number of characters you want to get back
-- TargetString: the stripped string to be returned
-- declare some variables
set TargetString to ""
-- store the wanted string
set TargetString to (characters 1 thru CharCount of StringSource) as string
-- return the wanted string
return TargetString
end StringLeft



-- ********** ********** ********** ********** **********
--
-- “StringRight(StringSource, CharCount)” handler definition
--
on StringRight(StringSource, CharCount)
-- StringSource: the string you want to extract characters from
-- CharCount: the number of characters you want to get back
-- TargetString: the stripped string to be returned
-- declare some variables
set TargetString to ""
set StringOffset to 0
set StringLength to 0
-- compute the source string length
set StringLength to count of characters in StringSource
-- compute the string offset
set StringOffset to (StringLength - CharCount) + 1
-- store the wanted string
set TargetString to (characters StringOffset thru ((StringOffset - 1) + CharCount) of StringSource) as string
-- return the wanted string
return TargetString
end StringRight


PS: if there is room to improve these handlers, please, share.

Reduced to their bare minimum:

	to StringLeft(s, n)
		return text 1 thru n of s
	end StringLeft

	to StringRight(s, n)
		return text -n thru -1 of s
	end StringRight

For StringRight, we're exploiting AppleScript's feature of negative indexes, which are defined to count backwards from the end of the container.


--Chris Nebel AppleScript Engineering

_______________________________________________
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: Now, I have “Left(StringSource, CharCount)” and “Right(StringSource, CharCount)”
      • From: "Mark J. Reed" <email@hidden>
References: 
 >Now, I have “Left(StringSource, CharCount)” and “Right(StringSource, CharCount)” (From: Emile Schwarz <email@hidden>)

  • Prev by Date: Re: Ma always said: there is a time to take and a time to give [Mid(Source, Start, End)]
  • Next by Date: Re: Now, I have “Left(StringSource, CharCount)” and “Right(StringSource, CharCount)”
  • Previous by thread: Now, I have “Left(StringSource, CharCount)” and “Right(StringSource, CharCount)”
  • Next by thread: Re: Now, I have “Left(StringSource, CharCount)” and “Right(StringSource, CharCount)”
  • Index(es):
    • Date
    • Thread