TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: MeasureUp on June 11, 2019, 03:25:03 AM

Title: How to Add Double Quotes in A Message
Post by: MeasureUp on June 11, 2019, 03:25:03 AM
Sorry I have not wrote codes for a long time.
Here is my question and below is a simplified case.
I'd like to show a message after user input:
Code: [Select]
(setq UserInput (getstring "Enter Letters: "))
(strcat "Your Input is " ... UserInput ... ".")

The message should be shown as below if the input was ABC.
Quote
Your Input is "ABC".

Your helps are much appreciated.
Title: Re: How to Add Double Quotes in A Message
Post by: kpblc on June 11, 2019, 03:40:36 AM
Code - Auto/Visual Lisp: [Select]
  1. (strcat "Your Inout is \"" UserInput "\".")
Title: Re: How to Add Double Quotes in A Message
Post by: Lee Mac on June 11, 2019, 08:31:01 AM
FWIW, you can determine the required escape characters by evaluating (getstring) at the AutoCAD command-line and entering the string that you wish to display - getstring will then return the string with the appropriate escape characters inserted for correct representation in AutoLISP, e.g.:
Code: [Select]
Command: (getstring t "\nEnter string: ")
Enter string: Your input is "ABC"
"Your input is \"ABC\""
Title: Re: How to Add Double Quotes in A Message
Post by: MeasureUp on June 11, 2019, 07:18:23 PM
Thanks to kpblc & Lee.
And Lee, you are too smart as always. :laugh: It is a good idea.
My confusion is the resault I got.
Quote
"Your input is \"ABC\"."
But when "princ" is added it shows correctly at command line.
Code: [Select]
(princ (strcat "Your Input is \"" UserInput "\"."))
Quote
Your input is "ABC".
Title: Re: How to Add Double Quotes in A Message
Post by: Lee Mac on June 12, 2019, 08:14:13 AM
My confusion is the resault I got.
Quote
"Your input is \"ABC\"."
But when "princ" is added it shows correctly at command line.
Code: [Select]
(princ (strcat "Your Input is \"" UserInput "\"."))
Quote
Your input is "ABC".

This is because ordinarily the double-quote character represents a string delimiter, signalling the start or end of a string literal. Therefore, in order to give this character an alternative meaning of 'display a literal double-quote' when evaluated by the AutoLISP interpreter, it is prefixed with the backslash escape character.

For more information & resources on this point, see my post here (https://www.cadtutor.net/forum/topic/49426-insert-quot-symbol-in-a-string/?tab=comments#comment-409254).
Title: Re: How to Add Double Quotes in A Message
Post by: frostmourn on June 13, 2019, 04:37:41 AM
Alternative:
Code - Auto/Visual Lisp: [Select]
  1. (princ "Your Input is ")
  2. (prin1 "ABC")
  3. (princ ".")
Title: Re: How to Add Double Quotes in A Message
Post by: MeasureUp on June 14, 2019, 02:15:51 AM
Thanks Lee.
And to frostmourn, agreed with the term of "alternative".