How can I write a newline in a string in ColdFusion?
Solution 1
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
Solution 2
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.
Solution 3
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" />
Solution 4
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.
Kip
I've been programming since I got my hands on a TI-83 in precalculus class during junior year of high school. Some cool stuff I've done: Chord-o-matic Chord Player: find out what those crazy chords are named! Everytime: keep track of the current time in lots of time zones from your system tray BigFraction: open source Java library for handling fractions to arbitrary precision. JSON Formatter: a completely client-side JSON beautifier/uglifier. QuickReplace: a completely client-side regex tool. It's behind some ugly developer UI since I created it for myself to use. (Sorry not sorry.)
Updated on December 12, 2020Comments
-
Kip over 3 years
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" />
-
Tomalak almost 15 yearsThis 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". :-)
-
brianestey almost 15 yearsMarc, let's do without the quotes and #'s- <cfset br = chr(13) & chr(10) />
-
Riot Goes Woof almost 11 yearsJust note that Chr(13) & Chr(10) doesnt work in a simple <cfoutput>. 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.
-
DaEagle almost 11 yearsIt 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 <br> ?
-
Riot Goes Woof almost 11 yearsPossibly. 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.
-
Vincent P over 9 yearsI 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.