CFLib.org – Common Function Library Project

padDirection(string, char, number[, charD])

Last updated May 09, 2003

author

Ron Gambill

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Basic padding function that allows user to decide which side of the submitted string that the characters should be added to (via L or R). See also padString().

Return Values:
Returns a string.

Example:

<cfset aString="Fred">
<cfoutput>
#padDirection(aString,"X",10,"R")#
</cfoutput>

Parameters:

Name Description Required
string String to pad. Yes
char Character to use as pad. Yes
number Number of characters to pad. Yes
charD Direction to pad. Should be "L" (left) or "R" (right). Defaults to R. No

Full UDF Source:

/**
 * Basic padding function that allows user to decide which side of the submitted string that the characters should be added to (via L or R).
 * 
 * @param string      String to pad. (Required)
 * @param char      Character to use as pad. (Required)
 * @param number      Number of characters to pad. (Required)
 * @param charD      Direction to pad. Should be "L" (left) or "R" (right). Defaults to R. (Optional)
 * @return Returns a string. 
 * @author Ron Gambill (rgambill@hotmail.com) 
 * @version 1, May 9, 2003 
 */
function padDirection(string,char,number) {
   // set up variables
   var strLen = len(string);
   var padLen = number - strLen;
   var returnValue = string;
   var charD = "R";

   if(arrayLen(arguments) gte 4) charD = arguments[4]; 
   if (strLen gte number) return string;
   
   if (charD eq "R") return string & RepeatString(char, padLen);
   else return RepeatString(char, padLen) & string;
}

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