Dave
Dave

Reputation: 987

Giving a string a specific length in coldfusion

Using coldfusion I would like to write a string to a txt document but it needs to be in a specific format

The code I am using at the moment is:

<!---SET STRING LENGTH FOR EACH--->
       <cfset initial = "#LJustify(initial, 35)#">
       <cfset lname_final = "#LJustify(lname_final, 35)#">
       <cfset accounttype_final = "#LJustify(accounttype_final, 35)#">
       <cfset amount_final = "#LJustify(amount_final, 35)#">
       <cfset date_final = "#LJustify(date_final, 35)#">

       <!---SET TOTAL STRING--->
       <cfset total_string = "#initial##lname_final##accounttype_final##amount_final##date_final#">

       #accountholder#<br>
       #accountnumber#<br>
       #accounttype#<br>
       #bankname#<br>
       #branch#<br>
       #amount#<br>
       #date#<br>
       #initial#<br>
       #lname_final#<br />
       #accounttype_final#<br>
       #amount_final#<br />
       #date_final#<br>
       123456789012345678901234567890123456789012345678901234567890<br />
       #total_string#<br>

When I run the code however it gives me the total_string as:

123456789012345678901234567890123456789012345678901234567890
G Sinclair CH 27500 01201212

I would like to achieve the following:

123456789012345678901234567890123456789012345678901234567890
GSinclair             CH                     2750001201212

So I need to space the values a certain amount from each other in a neat format within the txt document

So for example in the txt the rows should look like this:

123456789012345678901234567890123456789012345678901234567890
GSinclair             CH                     2750001201212
DGreen                OTH                    3456001201212
HRamsbottom           SAV                    0581620016181
GSmith                CC                     6326378734827

What is the best way to achieve this, should I loop an empty space depending on the length of each variable within the string or is there and easier way to achieve this?

Thanks in advance

Upvotes: 0

Views: 277

Answers (3)

krishna Ram
krishna Ram

Reputation: 659

Use

To make Text in the file as you need.

Like,

#accountholder##LF# #accountnumber##TAB##TAB##accounttype#

Upvotes: 1

David Jackson
David Jackson

Reputation: 46

You can also use the CHR for the tab space (#chr(9)#) to force a text editor to insert a tab. This won't help the browser (they ignore the tab command in general), but it will format the output for you in a text editor.

Upvotes: 1

Adam Cameron
Adam Cameron

Reputation: 29870

Well you're mixing the concepts of "plain text" (total_string) and "HTML" (the <br>) there. And you seem to be viewing your output in a browser, not in a text editor, because all the padding is there, just how you wanted it. However one of the behaviours of HTML rendering is that sequences of whitespace are collapsed by default. You can prevent this from happening in a browser by wrapping your string in <pre> tags.

However if you're wanting the output to be plain text, you should be saving it as a text document and inspecting it with a text editor, not with a browser.

Upvotes: 4

Related Questions