Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding line break after x number of characters 2

Status
Not open for further replies.

Floyd7

Programmer
May 23, 2007
35
0
0
US
Hi,

Do you know how I can add a line break to a string after a specificed number of characters?

Thank you!
 
<cfset myString = 'asdas wef3er gfwer arsfqase fqsdfqef '>

<cfset lineBreakPos = 20>

<cfset lineFeed = chr(13)&chr(10)>


<cfscript>
for (i=1; i lte len(myString); i=i+1)
{
if (i eq lineBreakPos)
{
myString = Insert(lineFeed, myString, lineBreakPos);
}
}
</cfscript>
<cfoutput>#htmlCodeFormat(myString)#</cfoutput>


 
If you want an html break you could add or replace with <br>
<cfset lineFeed = "<br />"&chr(13)&chr(10)>

ok and if you wanted it every 20 characters change to a mod function and use i

<cfscript>
for (i=1; i lte len(myString); i=i+1)
{
if (i mod lineBreakPos eq 0)
{
myString = Insert(lineFeed, myString, i);
}
}
</cfscript>
 
To insert a break _once_ after x characters, use Insert() as FALCONSEYE suggested. Just get rid of the loop. It is not needed.

<cfset newString = Insert(lineFeed, myString, 20)>

ok and if you wanted it every 20 characters change to a mod function and use i

Inserting a break after _every_ x characters is a little more complicated than that. As you insert characters into the string, you change the string and consequently its length. So you have to account for the new characters or the final position of the lineFeeds will be wrong.


----------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top