Rajesh Manilal
Rajesh Manilal

Reputation: 1124

ColdFusion function/logic to provide n number of white spaces between strings

I have to write a text file using ColdFusion. In that text file I need 'n' number of white spaces between strings.

For example:

'This<44 spaces>is<60 spaces>a<120 spaces>sampleText.'

So for this I'm using the ljustify() function in the places of white spaces, like

'This'&#ljustify(" ",44)#&'is'&#ljustify(" ",60)#&'a'&#ljustify(" ",120)#&'sampleText.'

I'm thinking that this will not be a coding standard. So, is there any other way to do this?

Upvotes: 2

Views: 578

Answers (1)

Fish Below the Ice
Fish Below the Ice

Reputation: 1271

It looks like what you want is RepeatString().

Creates a string that contains a specified number of repetitions of the specified string

Takes two parameters:

  1. A string (or a variable that contains one)
  2. Number of repeats

"This" & RepeatString(" ",44) & "is" & RepeatString(" ",60) & "a" & RepeatString(" ",120) & "sampleText."

Of course, you don't need to use a space. You can use RepeatString to repeat just about anything.

LJustify() is for "padding" a string with characters out to a set number of spaces.

Example:

[#LJustify("These",10)#]<br>
[#LJustify("are",10)#]<br>
[#LJustify("variable",10)#]<br>
[#LJustify("size",10)#]

would give you output like

[These     ]
[are       ]
[variable  ]
[size      ]

This is especially useful for creating fixed-length strings.

Upvotes: 4

Related Questions