Coldfusion: insert new line into string
I would like to insert a line break into开发者_JAVA百科 the first space between words in a string variable. Here is my code so far:
<cfset myPosition = find(" ", #myVar#)>
<cfset lineBreak = Chr(13)&Chr(10)>
<cfset myVar = insert(#lineBreak#, #myVar#, #myPosition#)>
What am I doing wrong?
I don't think that you are doing anything wrong. Your code seems to work. When you output your variable, try wrapping it in <pre></pre>
tags for testing purposes. If you want the linebreak to show up on a html page you have to replace the space with <br />
.
This works for me and shows the carriagereturn / linefeed:
<cfset myVar="The quick brown fox">
<cfset myPosition = find(" ", myVar)>
<cfset lineBreak = Chr(13) & Chr(10)>
<cfset myVar = insert(lineBreak, myVar, myPosition)>
<cfoutput>
<pre>#myVar#</pre>
</cfoutput>
BTW: there is no need to enclose your variables in #'s unless you want to output the variable or to have it evaluated between quotation marks.
精彩评论