• 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
A Replace() function
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A Replace() function


  • Subject: A Replace() function
  • From: Emile Schwarz <email@hidden>
  • Date: Wed, 18 Aug 2010 10:41:21 +0200

Hi all,

I needed a Replace() function to replace colon by dash and I failed to found it in the language.

So I searched in the Internet and found:

http://foolsworkshop.com/applescript/2008/05/an-applescript-replace-text-method/


I was not happy with that and wrote my own one (below).

Is it correct (it looks like that)?

TIA,

Emile

-- Syntax:
-- Result = Replace(SourceString, OldString, NewString)
--

on Replace(SourceStr, OldStr, NewStr)
	set LoopIdx to 1
	set NewSource to ""

	set SourceLen to count of SourceStr

	repeat SourceLen times
		set aChar to character LoopIdx of SourceStr
		if aChar = OldStr then
			set NewSource to NewSource & NewStr
		else
			set NewSource to NewSource & aChar
		end if

		set LoopIdx to LoopIdx + 1
	end repeat

	return NewSource
end Replace

-- Displays the time without colons
get Replace("10:28:35", ":", "-")
_______________________________________________
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: A Replace() function
      • From: KOENIG Yvan <email@hidden>
  • Prev by Date: Re: XML Error - any insight
  • Next by Date: Re: A Replace() function
  • Previous by thread: Re: XML Error - any insight
  • Next by thread: Re: A Replace() function
  • Index(es):
    • Date
    • Thread