• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
May 31, 2016 May 31, 2016

Copy link to clipboard

Copied

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.

Views

560

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 31, 2016 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

...

Votes

Translate

Translate
LEGEND ,
May 31, 2016 May 31, 2016

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 02, 2016 Jun 02, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 02, 2016 Jun 02, 2016

Copy link to clipboard

Copied

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 03, 2016 Jun 03, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 03, 2016 Jun 03, 2016

Copy link to clipboard

Copied

LATEST

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation