Skip to main content
Known Participant
February 2, 2007
Answered

line break

  • February 2, 2007
  • 5 replies
  • 703 views
How do you put a line break in a variable output. In coldfusion.

For example: variable jkjkjk4jkj4kj4kjk4jkj4k4jk4jk4jkjkjkjkjkjkjkjkjjkjkjk
But goes on horizontally

What if there is a variable or field that can out put 2000 characters or numbers but it shows all in one line and horizontally.
The problem with this is the users have to scroll left to see the end.

Is there a way to do it so it can break after a certain number of characters.
Or better yet goes to the next line without setting the numbers of characters to break.

Are there program examples for this?

Thanks
This topic has been closed for replies.
Correct answer mvierow
oops, make that

<cfset index = 1/>
<cfset maxLineLen = 40 />
<cfloop condition="index lt len(variable)">
<cfif len(variable) lt index + maxLineLen>
<cfoutput>#mid(variable,index,len(variable)-index)#</cfoutput><br/>
<cfelse>
<cfoutput>#mid(variable,index,maxLineLen)#</cfoutput><br/>
</cfif>
<cfset index = index + maxLineLen/>
</cfloop>

5 replies

BKBK
Community Expert
Community Expert
February 4, 2007
> Is there a way to do it so it can break after a certain number of characters.

Oh, yeah. It's a wrap().

mvierow
mvierowCorrect answer
Inspiring
February 3, 2007
oops, make that

<cfset index = 1/>
<cfset maxLineLen = 40 />
<cfloop condition="index lt len(variable)">
<cfif len(variable) lt index + maxLineLen>
<cfoutput>#mid(variable,index,len(variable)-index)#</cfoutput><br/>
<cfelse>
<cfoutput>#mid(variable,index,maxLineLen)#</cfoutput><br/>
</cfif>
<cfset index = index + maxLineLen/>
</cfloop>
mvierow
Inspiring
February 3, 2007
You could try adding the style "word-wrap: break-word;" to whatever container that text is in. Otherwise you would have to break the string up yourself, like so.

<cfset index = 1/>
<cfset maxLineLen = 40 />
<cfloop condition="index lt len(variable)">
<cfoutput>#mid(variable,index,maxLineLen)#</cfoutput><br/>
<cfif len(variable) lt index + maxLineLen>
<cfoutput>#right(variable,len(variable)-index-maxLineLen)#</cfoutput><br/>
</cfif>
<cfset index = index + maxLineLen/>
</cfloop>
Inspiring
February 2, 2007
The chr() function is what you want. Details are in the cfml reference manual. If you don't have one, the internet does.

Ascii codes are also on the internet somewhere
Inspiring
February 2, 2007
Wont it wrap if you put the output inside a table?