注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

不透明的雾的博客

人生是一次记忆的旅行

 
 
 

日志

 
 
 
 

Mimicking PHP's String Formatting Functions - str_pad  

2007-12-19 11:03:00|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
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!
  评论这张
 
阅读(280)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017