CFLib.org – Common Function Library Project

PadStringToLen(string, char, count)

Last updated June 18, 2002

author

Stephen Rittler

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Pads a string to a length of n characters. Padding is from the left. If the length of the string is greater than or equal to the number of characters to pad the string out to, the string is returned unchanged.

Return Values:
Returns a string.

Example:

<CFSET x=123>
<CFSET y="test">

<CFOUTPUT>
#PadStringToLen(x, 0, 8)#<BR>
#PadStringToLen(y, "a", 8)#<BR>
#PadStringToLen(y, "a", 2)#
</CFOUTPUT>

Parameters:

Name Description Required
string String you want to pad. Yes
char Character to use as the padding. Yes
count Total number of characters to pad the string out to. Yes

Full UDF Source:

/**
 * Pads a string to a length of n characters.  Padding is from the left.
 * Based on the UDF PadString() by Rob Brooks-Bilson (rbils@amkor.com).
 * 
 * @param string 	 String you want to pad. (Required)
 * @param char 	 Character to use as the padding. (Required)
 * @param count 	 Total number of characters to pad the string out to. (Required)
 * @return Returns a string. 
 * @author Stephen Rittler (scrittler@etechsolutions.com) 
 * @version 1, June 18, 2002 
 */
function PadStringToLen(string, char, count)
{
  var strLen = len(string);
  var padLen = count - strLen;
  if (padLen lte 0) {
    return string;
  }
  else {
    return RepeatString(char, padLen) & string;
  }
}
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