Path: utzoo!news-server.csri.toronto.edu!rutgers!ucsd!sdd.hp.com!think.com!barmar From: barmar@think.com (Barry Margolin) Newsgroups: comp.lang.lisp Subject: Re: strings with escape characters Keywords: strings, escape characters Message-ID: <1991Mar15.184822.17411@Think.COM> Date: 15 Mar 91 18:48:22 GMT References: <27E0EDEE.49E5@ibma0.cs.uiuc.edu> Sender: news@Think.COM Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 43 In article <27E0EDEE.49E5@ibma0.cs.uiuc.edu> murphy@ibma0.cs.uiuc.edu (michael r murphy) writes: >I would like to read in a C string and keep the escape characters >as is. I have enclosed a sample of my problem. What I would like >is: > > (setf foo "\nThis is a c string\n") > >"\nThis is a c string\n" > >I just want to get the \ in there without it being interpreted as >an escape. I can then handle printing it out. Help. You have to double the slash, i.e. (setf foo "\\nThis is a c string\\n") > (setf foo |"\nThis is a c string\n"|) >Error: Attempt to take the value of the unbound symbol |"nThis is a c stringn"| |...| is a symbol, not a string, so you have to use single-quote to prevent it from being evaluated. Also, backslash is interpreted inside vertical bars (to allow you to include a vertical bar as a character in a symbol name), so you still have to double it: (setf foo '|"\\nThis is a c string\\n"|) Note that in both cases there is only one backslash in the actual string. The doubling is only necessary during I/O. Notice: > (setq *string* "\\n") "\\n" > (length *string*) 2 > (coerce *string* 'list) (#\\ #\n) > (write-string *string*) \n "\\n" -- Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar