CFLib.org – Common Function Library Project

PadStringToLen(string, char, count)

Last updated June 18, 2002

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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 (rbils@amkor.comscrittler@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;
  }
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson