Count Number of Words in a Sentence using ASP

Date October 16, 2009

There is no function in ASP to count number of words in sentence or long string. Bellow is very simple function that count number of words and return that count.

< %
Function CountWords (StrValue)
  Dim iWords, mLengthWithSpaces, mLengthWithoutSpaces

  ' following line calculate the actual length of string
  mLengthWithSpaces = Len(StrValue)

  ' following line remove all spaces from string and then calculate the length of string
  mLengthWithoutSpaces = Len(Replace(StrValue, " ", ""))

  ' subtract length (without spaces) from actual string length
  iWords = mLengthWithSpaces - mLengthWithoutSpaces

  CountWords = iWords + 1
End Function

'example
Msg = "This is text string"
Response.Write CountWords(Msg)

'output will be 4
%>

Related Posts

Tags:

PoorFairInterestingGoodExcellent (3 votes, average: 4.67 out of 5)
Loading ... Loading ...

Leave a Reply


Most Rated Posts

Subscribe to Technology Tips

Subscribe to Saqib-Ansari via RSS

Or, subscribe via email: