ASP String Padding functions

ASP Best Practices

ASP left us in the lurch with string padding. These functions will left pad or right pad a string value to a given length.


Date : 2006-03-10
These string functions are available in other languages, but ASP left them out. Here's a little bit of fill-in for you.

Code:
Function Rpad (sValue, sPadchar, iLength)
  Rpad = sValue & string(iLength - Len(sValue), sPadchar)
End Function

Function Lpad (sValue, sPadchar, iLength)
  Lpad = string(iLength - Len(sValue),sPadchar) & sValue
End Function


These 2 functions are pretty straight forward. They will pad the string, sValue with sPadchar until the entire string length = iLength

so,
Rpad("1","0",100) would return a google.

Or you could do something more useful:
Lpad(Month(Date()),"0",2) to ensure that a month number would display as 2 digits.

If you enter more than one character in the sPadchar parameter it will only use the first character, so don't do that.

Hopefully these functions save you some time.

Comments :

Anonymous 2006-07-18 #28

Thanks! Saved me the time of figuring out my own functions to do this.

  • Search For Articles