How can I write a newline in a string in ColdFusion?

前端 未结 7 944
孤街浪徒
孤街浪徒 2021-02-03 20:58

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



        
相关标签:
7条回答
  • 2021-02-03 21:19

    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(

    0 讨论(0)
  • 2021-02-03 21:19

    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).

    0 讨论(0)
  • 2021-02-03 21:27

    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.

    0 讨论(0)
  • 2021-02-03 21:31

    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.

    0 讨论(0)
  • 2021-02-03 21:32

    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 讨论(0)
  • 2021-02-03 21:37

    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
    0 讨论(0)
提交回复
热议问题