36

Currently I'm putting newlines in strings through one of these two methods:

<cfset someStr="This is line 1" & Chr(10) & "This is line 2" & Chr(10) & "This is line 3" />

OR

<cfset NL=Chr(10) />
<cfset someStr="This is line 1#NL#This is line 2#NL#This is line 3" />

Is there anything more like the Java/C++ way? Something more like this I mean:

<cfset someStr="This is line 1\nThis is line 2\nThis is line 3" />
Kip
  • 107,154
  • 87
  • 232
  • 265

7 Answers7

38

Your way is correct. There is no support for \n or \r in CF. From the Live Docs

  • Chr(10) returns a linefeed character
  • Chr(13) returns a carriage return character
  • The two-character string Chr(13) & Chr(10) returns a Windows newline
Nick Van Brunt
  • 15,244
  • 11
  • 66
  • 92
  • 1
    Just note that Chr(13) & Chr(10) doesnt work in a simple . You need to have it in javascript or something else in order to get the newlines to show. I've been discovering this the fun way. – Riot Goes Woof Jul 09 '13 at 14:49
  • 3
    It works fine in a cfoutput for me. Of course it won't create a break in html because html treats it as just another whitespace but if you do a view source you would see the break. Maybe you just need a
    ?
    – Nick Van Brunt Jul 11 '13 at 18:23
  • Possibly. I just wanted to report that if people are having trouble with it, like I was, they should try putting it in javascript or something to see if that fixes it. – Riot Goes Woof Jul 12 '13 at 01:56
23

If you are into platform-independent development, you can do:

<cfset NL = CreateObject("java", "java.lang.System").getProperty("line.separator")>

For example, in your application.cfm/cfc or somewhere else high-level and use that.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • that's pretty cool. i'm new to CF, so i haven't really tapped the potential of the underlying java code yet... – Kip Jun 12 '09 at 18:01
  • 2
    I use this exclusively, its easy to say "we will only ever run on Windows", but it's going to be a sad day when you have to change hundreds of lines of code scattered about because you were lazy. – Vincent P Jan 07 '15 at 07:50
12

i use this:

<cfset br = "#chr(13)##chr(10)#">
<cfset someStr="This is line 1#br#This is line 2#br#This is line 3" />
marc esher
  • 4,871
  • 3
  • 36
  • 51
5

Not directly in CF, I'll leave it to the CF-Java dudes to say whether you can use a Java method directly on a CF var to achieve what you want, but...

You could use cfsavecontent to put natural line breaks in:

<cfsavecontent variable="someStr">
This is line 1
This is line 2
This is line 3
</cfsavecontent>

Then check it with:

<cfoutput>
<pre>#Trim(someStr)#</pre>
</cfoutput>

Note that the Trim() is there to get rid of the first and last line breaks if you don't want them.

Adrian Lynch
  • 8,237
  • 2
  • 32
  • 40
0

CF8 formatted cfmail with line feeds and without adding anything. Seems like Adobe would provide something SPECIFIC about "why" and a simple work-around. ... Jurisdictionary

0

I was looking for a way to output a new line in <cfscript>, so I figured I'd leave my answer for anyone else who arrived in a similar fashion:

writeDump(variable); // writeDump will not produce a new line.
writeOutput("<br>"); // you have to use writeOutput.

writeOutput appends to the page-output stream as html, so you need to write html for it to output (this means you can also include &nbsp; to add spaces for indentation).

Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
0

I was wondering if something like this would work:

<cfset str = CreateObject("java", "java.lang.String").init("Line 1\nLine 2\nLine 3")>

<cfoutput>
<pre>#str#</pre>
</cfoutput>

Alas no :O(

Adrian Lynch
  • 8,237
  • 2
  • 32
  • 40
  • 1
    This feeds a CF string to .init(). And a CF string does not understand "\n". That's a kind of problem I like to refer to as "can-opener in a can". :-) – Tomalak Jun 12 '09 at 16:22