To replace "#" with "\#"

Due to AS treats "" char as specific one I fails with replacing “#” to “#”

Is any idea to do it in AS/unix?

the replacement should be literally “#”

Hi,

try this


set t to "# this# is # a # test# "

set {TID, text item delimiters} to {text item delimiters, "#"}
set t to text items of t
set text item delimiters to "\\#"
set t to t as text
set text item delimiters to TID
t --> "\# this\# is \# a \# test\# " 

See also: text

Nitpick (with extra pickiness just for Stefan): Why use lists for part of that but not the rest?

set t to "# this# is # a # test# "

set {TID, text item delimiters of AppleScript} to {text item delimiters of AppleScript, "#"}
set {t, text item delimiters of AppleScript} to {text items of t, "\\#"}
set {t, text item delimiters of AppleScript} to {t as text, TID}
t --> "\# this\# is \# a \# test\# "

(Personally, I don’t prefer this style of list usage.)

Thanks!

I know about "" usage but I was muddled up with the fact that Script Debugger showed result in its Value column as “abc\#…\#…”

In general, is there a way to convert string with characters like "" etc. into unix-correct string?
And what the list of сhars need to be converted?

Very interesting, thank you Mr. Nitpicker :wink:

All AppleScript Editors display the escaped characters this way,
but if you write the result into a text file and open it e.g. with TextEdit, the backslashes are gone

Speaking as the inventor of the style (ie. using pattern assignment with TIDs) (though I can’t categorically say I was the first or only person to think of it!) I have to say I abandoned it years ago. It sacrifices legibility and speed for mere cleverness.