Skip to main content
Participating Frequently
May 31, 2016
Answered

Need to Insert linebreak after 64 letters in a long string.

  • May 31, 2016
  • 3 replies
  • 970 views

Hi Friends,

I am knew to CF - 11..I am trying to insert line break after 64 letters and dont know how to change below line. Kindly help. and also if possible please explain below line.

<cfset outstr &= Mid(instr,p,64) & #Chr(13)#>

Thanks

My code as follows.

<cfset instr = rereplace(arguments.value,"#chr(10)#","","all")>

    <cfset instr = rereplace(instr,"#chr(13)#","","all")>

  <cfset instr =trim(instr)>

<cfloop from="1" to="#len(instr)#" index="p" step="64">

<cfset outstr &= Mid(instr,p,64) & #Chr(13)#>

</cfloop>

currently I an getting a long string with a space after 64 letters as output.

This topic has been closed for replies.
Correct answer WolfShade

You could probably eliminate the cfloop altogether, since you're using the RegEx replace.

So, walk me through your code.

You're replacing all chr(10) and chr(13) with nothing, then you trim the string.  Then you loop and insert just a chr(13) every 64th position.

AFAIK, if you're using the Windows CF Server, both chr(13) AND chr(10) are the line break/carriage return codes.

Also, don't use hashmarks (#) for variables, unless the variable is either being output to the screen, or is inside the string delimiters.  (Doing <cfset outstr &= Mid(instr,p,64) & #chr(13)#> is not advised.  Remove the #'s around the chr(13).)

Hmm.. I haven't tested this, but try the following:

<cfset instr = trim(rereplace(arguments.value,"#chr(13)&chr(10)#|#chr(13)#|#chr(10)#","","all")) />

<!--- One way of removing all line breaks, and trimming; there are others --->

<cfset instr = rereplace(instr,"(.){64}","\1#chr(13) & chr(10)#","all") />

<!--- \1 is the backreference; equivalent to JavaScript RegEx $1 --->

Let me know if that works.

HTH,

^_^

UPDATE: I had a chance to test it, and with two minor corrections, it works.

<cfset instr = rereplace(instr,"(.{64})","\1#chr(13) & chr(10)#<br/>","all") />  The <br /> will display line breaks in the browser, but without it you can see the line breaks if you "View Source" the page. 

3 replies

Participating Frequently
June 3, 2016

Thank you so much WolfShade..The issue resolved now..I missed chr(10) in a place which caused the issue.Thanks a lot for you timely help

WolfShade
Brainiac
June 3, 2016

Glad I could help.  BTW, where was is chr(10) that you missed?

If you could, please, mark my answer as correct, in case anyone else has the same question.

V/r,

^_^

Participating Frequently
June 2, 2016

Thank a lot WolfShade  .Even though its working fine in browser. while downloading the certificate the format is not aligned. I am getting single line instead of (64 letters in a line) and a space after each 64 letters

WolfShade
Brainiac
June 2, 2016

How are you using the resulting string?  The <br />, if you included it, may be messing things up.

Conversely, if you are just outputting the text to a browser, then check the "View Source", because a line break in the code will appear as a space in the browser, in which case the <br /> would be necessary to force the sections onto their own line in the browser display.

HTH,

^_^

WolfShade
WolfShadeCorrect answer
Brainiac
May 31, 2016

You could probably eliminate the cfloop altogether, since you're using the RegEx replace.

So, walk me through your code.

You're replacing all chr(10) and chr(13) with nothing, then you trim the string.  Then you loop and insert just a chr(13) every 64th position.

AFAIK, if you're using the Windows CF Server, both chr(13) AND chr(10) are the line break/carriage return codes.

Also, don't use hashmarks (#) for variables, unless the variable is either being output to the screen, or is inside the string delimiters.  (Doing <cfset outstr &= Mid(instr,p,64) & #chr(13)#> is not advised.  Remove the #'s around the chr(13).)

Hmm.. I haven't tested this, but try the following:

<cfset instr = trim(rereplace(arguments.value,"#chr(13)&chr(10)#|#chr(13)#|#chr(10)#","","all")) />

<!--- One way of removing all line breaks, and trimming; there are others --->

<cfset instr = rereplace(instr,"(.){64}","\1#chr(13) & chr(10)#","all") />

<!--- \1 is the backreference; equivalent to JavaScript RegEx $1 --->

Let me know if that works.

HTH,

^_^

UPDATE: I had a chance to test it, and with two minor corrections, it works.

<cfset instr = rereplace(instr,"(.{64})","\1#chr(13) & chr(10)#<br/>","all") />  The <br /> will display line breaks in the browser, but without it you can see the line breaks if you "View Source" the page.