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);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson