MattsBits
MattsBits

MattsBits

Javascript Functions To Left And Right Pad A String  

by Matt Hawkins, 14/10/2010
Categories : JavaScript

The following functions allow you to Left or Right pad a string with a specified number of characters.

LPad(pStart,pTotalSize,pPad)

RPad(pStart,pTotalSize,pPad)


<script type="text/javascript">
<!--

// These functions return strings padded with
// the appropriate number of padding characters
// either on the left or right of the starting
// string.
//
// pStart - Starting string or number
// pTotalSize - Total length of completed string
// pPad - Character to use for padding
//
// http://www.mattbits.co.uk

function LPad(pStart,pTotalSize,pPad) {

var vPadding;

pStart = pStart.toString();

if (pTotalSize > pStart.length)
{
for (i=0; i < (pTotalSize-pStart.length); i++)
{
vPaddedString += pPad;
}
}
return vPadding + pStart.toString();
}

function RPad(pStart,pTotalSize,pPad) {

var vPadding;

pStart = pStart.toString();

if (pTotalSize > pStart.length)
{
for (i=0; i < (pTotalSize-pStart.length); i++)
{
vPaddedString += pPad;
}
}
return pStart.toString() + vPadding;
}

//-->
</script>

Author : Matt Hawkins  Last Edit By : Matt Hawkins
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  Firefox - Take Back The Web  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v3.0 - Copyright 2009-2022