CFLib.org – Common Function Library Project

cjustify2(string, length[, padChar])

Last updated September 16, 2012

author

Al Everett

Version: 1 | Requires: CF5 | Library: StrLib

Description:
This function mimics the CJustify() function, but offers an optional third parameter to define the character to use for padding (if padding is necessary).

Return Values:
A string, center-justified to occupy the specified field length

Example:

<cfoutput>
#cjustify2("foobar",10,".")#<br> <!--- outputs: ..foobar.. --->
#cjustify2("foobar",5,"-")#<br> <!--- outputs: foobar --->
</cfoutput>

Parameters:

Name Description Required
string String to justify Yes
length Length of field Yes
padChar Character to use for padding No

Full UDF Source:

/**
 * Same as built-in CJustify(), but allows optional parameter character to pad with.
 * version 1.0 by Al Everett
 * 
 * @param string      String to justify (Required)
 * @param length      Length of field (Required)
 * @param padChar      Character to use for padding (Optional)
 * @return A string, center-justified to occupy the specified field length 
 * @author Al Everett (everett.al@gmail.com) 
 * @version 1.0, September 16, 2012 
 */
function cJustify2(string,length) {
    var padChar = " ";
    var padLeftCount = 0;
    var padRightCount = 0;
    if (arrayLen(arguments) GT 2) {
        padChar=left(arguments[3],1);
    }
        
    if (len(string) LT length) {
        padLeftCount = (arguments.length - len(arguments.string)) \ 2; // integer divide by 2 the number of characters for padding
        padRightCount = arguments.length - len(arguments.string) - padLeftCount; // take whatever is left over and put on the right
    }
        
    return repeatString(padChar,padLeftCount) & arguments.string & repeatString(padChar,padRightCount);
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson