Re: Find and replace string
Re: Find and replace string
- Subject: Re: Find and replace string
- From: "Gary (Lists)" <email@hidden>
- Date: Tue, 01 Nov 2005 13:55:10 -0500
"Steve Foster" wrote:
> Hi
>
> I am trying to work out a simple find and replace script.
>
> I need it to replace a ^ with a carriage return.
>
> I am a VBScript developer and would usually use something like:
>
> ____________________________________
> strString = "Hello^Mum"
>
> strString = Replace(strString, "^", vbcrlf)
> ____________________________________
>
> Everything I can find on the list seems overly complicated for such a simple
> task (using RegExp and shell script).
>
> Any ideas?
In AppleScript, if you want to break a string at some delimiter, you use the
"text item delimiters". Here's an AS example based on your vb example.
set strString to "Hello^Mum"
set the text item delimiters to {"^"}
set strParts to text items of strString
set the text item delimiters to return
set newString to strParts as string
-- restore, if you like
set the text item delimters to {""}
If you keep a set of ready-to-use handlers, around, then you could...
set strString to "Hello^Mum"
set newString to join(return, split("^", strString))
...where 'split()' and 'join()' might look like this:
-- --------------------------------------------------------
to split(needle, haystack) -- delim, string
set oTIDs to the text item delimiters
set the text item delimiters to needle
set hay to (text items of haystack)
set the text item delimiters to oTIDs
return hay
end split
-- --------------------------------------------------------
to join(glue, chunks) -- delim, list
set oTIDs to the text item delimiters
set the text item delimiters to glue
set squished to ((text items of chunks) as string)
set the text item delimiters to oTIDs
return squished
end join
-- --------------------------------------------------------
HTH,
--
Gary
_______________________________________________
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