Cuis-Automatically escape a complex string: Difference between revisions

From Cuis CookBook
Jump to navigation Jump to search
(added link)
No edit summary
Line 5: Line 5:
* Suppose the string you want to quote is <code>echo "i'm nicola, who are you?"</code>
* Suppose the string you want to quote is <code>echo "i'm nicola, who are you?"</code>
* Open a TextMorph
* Open a TextMorph
<syntaxhighlight lang="smalltalk">

te _ TextEditor openTextEditor .
te := TextEditor openTextEditor .
</syntaxhighlight>

* Write the string into it
* Write the string into it
* Go back to the Workspace and type this
* Go back to the Workspace and type this
<syntaxhighlight lang="smalltalk">

te model actualContents asString . => 'echo "hello, i<nowiki>''</nowiki>m nicola, who are you?"'
te model actualContents asString . => 'echo "hello, i''m nicola, who are you?"'
</syntaxhighlight>

* Voila<nowiki>', you have your string automatically escaped, notice the doulbe $' character in i''</nowiki>m nicola.
* Voila<nowiki>', you have your string automatically escaped, notice the doulbe $' character in i''</nowiki>m nicola.
* Some time ago I made a [https://youtu.be/clJIJnQ5vQ4 video tutorial] about this with a much worst string. There the procedure is a bit different, it was specific for Squeak.
* Some time ago I made a [https://youtu.be/clJIJnQ5vQ4 video tutorial] about this with a much worst string. There the procedure is a bit different, it was specific for Squeak.

Revision as of 22:29, 4 May 2025

Problem You have a long shell command, or an SQL command, or something else which may contain tne $' Character. You can escape the $' by doubling it yourself but it will become quite hard to read your original string. Is there a better way?

Solution. Yes, there is a nice trick to achieve that.

  • Suppose the string you want to quote is echo "i'm nicola, who are you?"
  • Open a TextMorph
te := TextEditor openTextEditor .
  • Write the string into it
  • Go back to the Workspace and type this
te model actualContents asString . => 'echo "hello, i''m nicola, who are you?"'
  • Voila', you have your string automatically escaped, notice the doulbe $' character in i''m nicola.
  • Some time ago I made a video tutorial about this with a much worst string. There the procedure is a bit different, it was specific for Squeak.

Dr. Nicola Mingotti firt wrote this on 21-Sep-2021. Tested in Cuis5.0-4834.image