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

不透明的雾的博客

人生是一次记忆的旅行

 
 
 

日志

 
 
 
 

Mimicking PHP's String Formatting Functions - str_word_count and ucfirst  

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

  下载LOFTER 我的照片书  |
Let me explain the str_word_count function: It counts words.
Yes, simple as that, you feed it a string, and it feeds you back the number of separate words within the string. Here's how we could duplicate it in ASP:

'==================================================
Function strWordCount( strToCount )
'==================================================
Dim arWords

strToCount = Trim( strToCount & "" )
arWords = split( strToCount, " " )
strWordCount = uBound( arWords ) + 1

End Function

Yes, it is really very simple. It just breaks the string by its spaces, as spaces logically define the separation of words. These are broken into an array, and the array is counted to see how many words it's holding. Of course we need to add one to the number, as arrays are zero-based, and we're not.
This will even work if you hand it a string with no spaces. It will not break, the array will contain the entire string as the first item of the array, and the word count will be one. Also, the first line of the function prevents any runtime errors due to handing the function a null value, and it does this by turning it into a string.
So that was simple, let's move on to the next two.
Sometimes it is very important to have proper formatting in a string. One example is when a user enters their City into a form, you may want to match it to a city on your database. It is essential to ensure that you establish and enforce proper formatting. In most cases it would be to have the first letter capitalized, and the rest lowercase. Yes, we can prevent problems caused by the angry people who type in all caps, or the lazy ones like myself who never hit the shift key!
PHP has a great little function call uc_first, which returns a string formatted exactly as I described. Here's a simple way to do exactly the same thing with ASP.

'===================================================
Function ucFirst( strWord )
'===================================================
strWord = trim( strWord & "" )
if len( strWord ) > 0 then
ucFirst = uCase( left( strWord, 1 ) ) & _
lcase( right( strWord, len( strWord ) - 1 ) )
end if
End Function

Going a step further, it was realized that you may have a string containing many words, and you want them ALL to be formatted in this fashion. A good example of this is a title. No matter how a title has been entered, you still want it to look like a title, and so we shall:

'===================================================
Function ucWords( strWords )
'===================================================
strWords = trim( strWords & "" )
if len( strWords ) > 0 then
dim arWords, i, strFormatted
arWords = split( strWords, " " )
for i = 0 to uBound( arWords )
strFormatted = strFormatted & " " & ucFirst( arWords( i ) )
next
ucWords = strFormatted
end if
End Function

So if you handed this function a string like this:
response.write( "i dON'T liKe thiS ARTICLE At all" )
It would come back formatted properly, and the output should be:
"I Love This Article"
Hmm, wishful thinking... if only my functions were that smart! Well then, let's move on.
  评论这张
 
阅读(91)| 评论(0)
推荐 转载

历史上的今天

评论

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

页脚

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