Mimicking PHP's String Formatting Functions - str_pad
(Page 4 of 4 )
PHP has any interesting little function called str_pad. You basically hand any string to the function with the desired length, and it appends spaces to the string until reaching that length. You have the options of specifying the characters to append instead of spaces, and also where to pad (left, right, or both). This would be handy for padding numbers with leading zeros, and perhaps you have other uses.
Unfortunately, ASP does not allow for optional parameters. So if you want to use the default padding of a space, and the default location of padding to the right, just throw in empty quotations as parameters.
There are two functions referenced in the code that may be slightly foreign to you, (isTrulyNumeric & errorMessage) but you can find them explained at: http://www.aspfree.com/c/a/ASP/Easy-Error-Management/
'===========================================
Function strPad( strToPad, toLength, padWith, padWhere )
'===========================================
'padWhere: 1=right, 2=left, 3=both
strToPad = strToPad & ""
dim padBoth
padBoth = false
if not isTrulyNumeric( toLength ) then
call errorMessage( "Desired length not specified!")
else
toLength = cint( toLength )
end if
if padWith = "" then padWith = " "
if not isReallyNumeric( padWhere ) then
padWhere = 1
else
padWhere = cInt( padWhere )
if padWhere > 3 or padWhere < 1 then padWhere = 1
if padWhere = 3 then
padBoth = true
padWhere = 1
end if
end if
do until len( strtoPad ) >= toLength
select case padWhere
case 1
strtoPad = strtoPad & padWith
case 2
strtoPad = padWith & strtoPad
end select
if padBoth then
if padWhere = 1 then
padWhere = 2
else
padWhere = 1
end if
end if
loop
if len( strToPad ) > toLength then
if ( padBoth and padWhere = 1 ) or ( padWhere = 2 and not padBoth ) then '=== no carriage return here
strToPad = right( strToPad, toLength )
else
strToPad = left( strToPad, toLength )
end if
end if
strPad = strToPad
End Function
So you can see that if it's specified to pad on both left and right, the do loop simply alternates between left and right. At the end of it all, if the resulting string is longer than the desired length, it's trimmed in the proper location.
Therefore, typing in:
response.write( strPad( "dog", 10, "CAT", "" ) )
will output:
dogCATCATC
Conclusion
I hope this article has taught you a very important lesson, ALWAYS look both ways before crossing the road! No wait, wrong lesson. I meant to say, ALWAYS look for a creative and reusable solution to your problem, and if you see something prepackaged in another language or environment, why not make your own version of it?
You should be able to find lots of uses for these functions, but I also hope you find success in creating your own custom (and maybe borrowed) functions as well. Like I mentioned before, feel free to share them in the forum!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |