Efficient way of editing a string?

Is there an efficient way of editing a sting?

the only way i can do it right now is either

set x to "hello"
set y to character 1 of x & character 2 of x & "z" & character 4 of x & character 5 of x

or

set x to "hello"
set y to text returned of (display dialog "" default answer x)

but the first one seems really inefficient and the second one requires user input

Hello.

You aren’t actually editing a string inside the string , you are effectively assembling a copy of the original string, even if you assign the result to the same placeholder, or label as someone would call it.

Secondly, you haven’t actually much leeway for editing strings, but there are text item delimiters

set s to "there are squibbles in our ears that confuses us"

if "us" is in s then
	set mentioned to true
	set ourPos to (offset of "us" in s)
	--> 42!
	set theWords to words of s
	set something to text 1 thru word 3 of s & text ((offset of (word 7 of s) in s) - 1) thru (word -1 of s) of s
	--> "there are squibbles that confuses us"
	-- using text item delimiters
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " in our ears"}
	set coolStuff to text items of s
	set AppleScript's text item delimiters to ""
	set theSame to coolStuff as text
	--> "there are squibbles that confuses us"
	set AppleScript's text item delimiters to tids
end if

I urge you to read the text item delimiters tutorial you can find in the "Unscripted section of this site! :slight_smile:

Hi.

That’s a somewhat vague question. How you edit text obviously depends on the rationale behind the edit. If the rationale is simply, as in your example, "I have a five-character text and I want a similar text but with the third character being “z”, the most efficient way would be:

set x to "hello"
set y to text 1 thru 2 of x & "z" & text 4 thru 5 of x