开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜