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

Discussion of Common Lisp
Post Reply
punchcard
Posts: 16
Joined: Fri Sep 26, 2014 5:50 pm

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

Post by punchcard » Sat Oct 04, 2014 4:59 am

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:
Last edited by punchcard on Sun Oct 05, 2014 12:41 am, edited 1 time in total.

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

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

Post by edgar-rft » Sat Oct 04, 2014 6:08 am

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.

punchcard
Posts: 16
Joined: Fri Sep 26, 2014 5:50 pm

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

Post by punchcard » Sat Oct 04, 2014 6:12 am

:D Thank you! thats great.

Post Reply