Character Count


/ Published in: ASP
Save to your folder(s)

Count the Number of Occurrences of a Character Sequence in a String


Copy this code and paste it in your HTML
  1. <%
  2. 'Counts number of occurrences of a certain char(s) in a string, returns number
  3. Function CharCount(SourceString, CharsToFind)
  4.  
  5. LengthOfSourceString = Len(SourceString)
  6. LengthOfCharsToFind = Len(CharsToFind)
  7.  
  8. If LengthOfCharsToFind >= LengthOfSourceString Then
  9. If LCase(CharsToFind) = LCase(SourceString) Then
  10. CharCount = 1
  11. Else
  12. CharCount = 0
  13. End If
  14. Exit Function
  15. Else
  16. LoopLength = (LengthOfSourceString - LengthOfCharsToFind) + 1
  17. OccurrenseCounter = 0
  18. For i=1 to LoopLength
  19. CurrentSample = Mid(SourceString,i,LengthOfCharsToFind)
  20. If LCase(CurrentSample) = LCase(CharsToFind) Then
  21. OccurrenseCounter = OccurrenseCounter + 1
  22. Else
  23. End If
  24. Next
  25. CharCount = OccurrenseCounter
  26. End If
  27.  
  28. End Function
  29. %>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.