Posted By

Shurik76 on 07/17/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

fwd


Character Count


 / Published in: ASP
 

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

  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  

You need to login to post a comment.