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;
  }
}

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