From: Ray Fink
Subject: Re: How do I create a string with #\Newline  in it?
Date: 
Message-ID: <117kt0nfacp3l7b@news.supernews.com>
using the ~% macro in format

[2]> (setf s (format nil "A string~% with some ~%linefeeds.~%"))
"A string
  with some
linefeeds.
"

-- Ray

Jeffrey Cunningham wrote:
> If I want to create a string with linefeeds in it in, say, C, I can do
> this:
> 
> const char* s="A string\nwith some \nlinefeeds.\n"
> 
> When I try something like this in Lisp, the interpeter only expands the
> first letter:
> 
> (setf s "A string#\Newlinewith some #\Newline.#\Newline)
> 
> => "A stringNewlinewith some Newlines.N"
> 
> I can get what I want using a (concatentate 'string) kludge, or by after
> the fact character substitution using char. Somehow I doubt this is how
> it should be done, but I have yet to see any examples. 
> 
> How do you do this?
> 
> -Jeff
> 

From: Pascal Bourguignon
Subject: Re: How do I create a string with #\Newline  in it?
Date: 
Message-ID: <87u0lh1j9g.fsf@thalassa.informatimago.com>
Ray Fink <···@cableone.THIS_PART_IS_BOGUS.net> writes:

> using the ~% macro in format
>
> [2]> (setf s (format nil "A string~% with some ~%linefeeds.~%"))
> "A string
>   with some
> linefeeds.

No, ~% generates a new line not a line feed.

(defparameter s (format nil "A string~Cwith some~Cline-feeds.~%"
                        #\linefeed #\linefeed))

Otherwise, you can just type the line-feed inside the literal string:

$ od -c /tmp/s.lisp
0000000   (   d   e   f   p   a   r   a   m   a   e   t   e   r       s
0000020       "   A       s   t   r   i   n   g  \n   w   i   t   h    
0000040   s   o   m   e  \n   l   i   n   e   -   f   e   e   d   s   ,
0000060       a   n   d       c   a   r   r   i   a   g   e   -   r   e
0000100   t   u   r   n   s  \r   a   n   d       c   r   -   l   f    
0000120   s   e   q   u   e   n   c   e   s  \r  \n   a   n   d       t
0000140   h   e   r   e  \r  \n   E   O   T   .   "   )  \n
0000155
$ cat -A /tmp/s.lisp
(defparamaeter s "A string$
with some$
line-feeds, and carriage-returns^Mand cr-lf sequences^M$
and there^M$
EOT.")$


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

This is a signature virus.  Add me to your signature and help me to live
From: Barry Margolin
Subject: Re: How do I create a string with #\Newline  in it?
Date: 
Message-ID: <barmar-42ED69.21045705052005@comcast.dca.giganews.com>
In article <··············@thalassa.informatimago.com>,
 Pascal Bourguignon <···@informatimago.com> wrote:

> Ray Fink <···@cableone.THIS_PART_IS_BOGUS.net> writes:
> 
> > using the ~% macro in format
> >
> > [2]> (setf s (format nil "A string~% with some ~%linefeeds.~%"))
> > "A string
> >   with some
> > linefeeds.
> 
> No, ~% generates a new line not a line feed.

The OP was probably thinking of systems where newline is represented 
with linefeed, since he said "Newline" in his subject line, and 
"linefeed" in the text.  I think we can presume that "newline" is what 
he really meant, since that also corresponds to what the C code he 
wanted to translate to Lisp does.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Jeffrey Cunningham
Subject: Re: How do I create a string with #\Newline  in it?
Date: 
Message-ID: <pan.2005.05.06.02.08.37.245873@cunningham.net>
On Thu, 05 May 2005 21:04:57 -0400, Barry Margolin wrote:


> The OP was probably thinking of systems where newline is represented
> with linefeed, since he said "Newline" in his subject line, and
> "linefeed" in the text.  I think we can presume that "newline" is what
> he really meant, since that also corresponds to what the C code he
> wanted to translate to Lisp does.

Yes, I was looking for whatever one should call (code-char 10), so that
the text thus generated would be correctly interpreted by scripts on unix
systems as a linefeed. I thought that was (again - on unix systems) the
same thing as a Lisp #\Newline?  Or is this a function of
implementation/OS?

In any event, I thank you gentlemen - one and all. Great forum.

-jeff
From: Barry Margolin
Subject: Re: How do I create a string with #\Newline  in it?
Date: 
Message-ID: <barmar-8291B1.22380805052005@comcast.dca.giganews.com>
In article <······························@cunningham.net>,
 Jeffrey Cunningham <·······@cunningham.net> wrote:

> On Thu, 05 May 2005 21:04:57 -0400, Barry Margolin wrote:
> 
> 
> > The OP was probably thinking of systems where newline is represented
> > with linefeed, since he said "Newline" in his subject line, and
> > "linefeed" in the text.  I think we can presume that "newline" is what
> > he really meant, since that also corresponds to what the C code he
> > wanted to translate to Lisp does.
> 
> Yes, I was looking for whatever one should call (code-char 10), so that
> the text thus generated would be correctly interpreted by scripts on unix
> systems as a linefeed. I thought that was (again - on unix systems) the
> same thing as a Lisp #\Newline?  Or is this a function of
> implementation/OS?

It's implementation-specific.  On a Mac I think it would be (code-char 
13), since newline is Carriage Return.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Kent M Pitman
Subject: Re: How do I create a string with #\Newline  in it?
Date: 
Message-ID: <uy8atdkm6.fsf@nhplace.com>
Barry Margolin <······@alum.mit.edu> writes:

> In article <······························@cunningham.net>,
>  Jeffrey Cunningham <·······@cunningham.net> wrote:
> 
> > On Thu, 05 May 2005 21:04:57 -0400, Barry Margolin wrote:
...
> > Yes, I was looking for whatever one should call (code-char 10), so that
> > the text thus generated would be correctly interpreted by scripts on unix
> > systems as a linefeed. I thought that was (again - on unix systems) the
> > same thing as a Lisp #\Newline?  Or is this a function of
> > implementation/OS?
> 
> It's implementation-specific.  On a Mac I think it would be (code-char 
> 13), since newline is Carriage Return.

The semi-standard #\Return and #\Linefeed are widely supported and 
more reliable in some contexts, such as the writing of network protocols.
You want #\Linefeed, not #\Newline.
From: Jeffrey Cunningham
Subject: Re: How do I create a string with #\Newline  in it?
Date: 
Message-ID: <pan.2005.05.07.14.21.50.446797@cunningham.net>
On Fri, 06 May 2005 04:47:18 +0000, Kent M Pitman wrote:

> Barry Margolin <······@alum.mit.edu> writes:
> 
> The semi-standard #\Return and #\Linefeed are widely supported and more
> reliable in some contexts, such as the writing of network protocols. You
> want #\Linefeed, not #\Newline.

So If I want to parse a string for whitespace, for example, a reasonable
way to do this would be something like:

(defparameter *whitespace* (format nil " ~C~C~C" #\Linefeed #\Return
#\Tab))

(search *whitespace* some-string)

--jeff
From: Pascal Bourguignon
Subject: Re: How do I create a string with #\Newline  in it?
Date: 
Message-ID: <87mzr7xeyf.fsf@thalassa.informatimago.com>
Jeffrey Cunningham <·······@cunningham.net> writes:

> On Fri, 06 May 2005 04:47:18 +0000, Kent M Pitman wrote:
>
>> Barry Margolin <······@alum.mit.edu> writes:
>> 
>> The semi-standard #\Return and #\Linefeed are widely supported and more
>> reliable in some contexts, such as the writing of network protocols. You
>> want #\Linefeed, not #\Newline.
>
> So If I want to parse a string for whitespace, for example, a reasonable
> way to do this would be something like:
>
> (defparameter *whitespace* (format nil " ~C~C~C" #\Linefeed #\Return
> #\Tab))
>
> (search *whitespace* some-string)


[270]> (search "bee" "zaphod beeblebrox")
7
[271]> (search "bee" "abbbdeeehlooprxz")
NIL

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
I need a new toy.
Tail of black dog keeps good time.
Pounce! Good dog! Good dog!
From: Jeffrey Cunningham
Subject: Re: How do I create a string with #\Newline  in it?
Date: 
Message-ID: <pan.2005.05.09.05.02.28.367616@cunningham.net>
On Sat, 07 May 2005 16:56:08 +0200, Pascal Bourguignon wrote:

> [270]> (search "bee" "zaphod beeblebrox")
> 7
> [271]> (search "bee" "abbbdeeehlooprxz")
> NIL

Ahhh (light goes on). Thanks.

-jeff
From: Steven M. Haflich
Subject: Re: How do I create a string with #\Newline  in it?
Date: 
Message-ID: <NSxfe.328$Y81.137@newssvr21.news.prodigy.com>
Jeffrey Cunningham wrote:

 > So If I want to parse a string for whitespace, for example, a reasonable
 > way to do this would be something like:
 >
 > (defparameter *whitespace* (format nil " ~C~C~C" #\Linefeed #\Return 
#\Tab))
 >
 > (search *whitespace* some-string)

Not in Common Lisp!  It is not required that the character names
Linefeed, Return, of Tab mean anything in ANSI CL.  Despite the
fact that most implementations define them, reading these character
names _might_ signal error in smoe conforming CL.
From: Joerg Hoehle
Subject: Re: How do I create a string with #\Newline  in it?
Date: 
Message-ID: <uvf5a5401.fsf@users.sourceforge.net>
Jeffrey Cunningham <·······@cunningham.net> writes:
> (defparameter *whitespace* (format nil " ~C~C~C" #\Linefeed #\Return
> #\Tab))
> 
> (search *whitespace* some-string)

I find
(map 'string #'code-char '(9 10 12 13 #x20))
more readable, or
(coerce '(#\Linefeed #\Return #\Space #\Tab) 'string)

but the latter uses non-portable character names. Lisp implementations
don't even agree on whether #\NUL or #\NULL names (code-char 0).
(IMHO, NUL is correct w.r.t. ASCII, NULL is sign of the
programmer's brain infected by C ;-) Of course, YMMV

Of course, FORMAT friends will point at
(format nil "~{~C~}" '(#\Tab ...))
or
(format nil ··@{~C~}" #\tab #\Return ...)

Regards,
	Jorg Hohle
Telekom/T-Systems Technology Center