Page 1 of 1

how to print and store the " character in strings [SOLVED]

Posted: Sat Oct 04, 2014 4:59 am
by punchcard
I have been looking through the Lisp 3rd edition and trying to find some way on the net... is there a way I can print the " character or store it as a part of the data in a string?


what I want to be able to do is ( " The man said "hello world" and went to his car. " ).

Is there some way to print a character code by code number?

*update

I actually later found the part of Lisp 3rd ed that covered this - it was buried in the Classes and Generic Functions chapter (c14, p191). Always the way when you are looking for somthing else the thing you were looking for first turns up. :lol:

Re: how to print and store the " character in strings

Posted: Sat Oct 04, 2014 6:08 am
by edgar-rft
The usual trick is the same as in most other programming langauges, just simply escape double-quotes with backslashes:

Code: Select all

CL-USER> (princ "The man said \"hello world\" and went to his car.")
The man said "hello world" and went to his car.       ; printed value using PRINC
"The man said \"hello world\" and went to his car."   ; returned value

CL-USER> (prin1 "The man said \"hello world\" and went to his car.")
"The man said \"hello world\" and went to his car."   ; printed value using PRIN1
"The man said \"hello world\" and went to his car."   ; returned value
... or in FORMAT syntax:

Code: Select all

CL-USER> (format t "~a~%" "The man said \"hello world\" and went to his car.")
The man said "hello world" and went to his car.       ; printed value "~a"
NIL                                                   ; returned value

CL-USER> (format t "~s~%" "The man said \"hello world\" and went to his car.")
"The man said \"hello world\" and went to his car."   ; printed value "~s"
NIL                                                   ; returned value
This also works if you store the string in a variable:

Code: Select all

CL-USER> (let ((my-string "The man said \"hello world\" and went to his car."))
           (format t "~a~%" my-string)
           (format t "~s~%" my-string))
The man said "hello world" and went to his car.       ; printed value "~a"
"The man said \"hello world\" and went to his car."   ; printed value "~s"
NIL                                                   ; returned value
In case of doubt see PRIN1, PRINC, and FORMAT.

Re: how to print and store the " character in strings

Posted: Sat Oct 04, 2014 6:12 am
by punchcard
:D Thank you! thats great.