faqts : Computers : Programming : Languages : Asp : ASP/VBScript : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

79 of 89 people (89%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I count the occurrence of characters in a string?

Jul 13th, 2000 23:45
Rory Knowles, unknown unknown, Michael S. Comperchio


Here are 3 different character counts..
<%
'#1 - count total number of characters including spaces:
'-------------------------------------------------------
 strRequest = "This is One string Number is Total of 40"
 response.write Len(strRequest)
'-------------------------------------------------------
'#2 - count number of characters without spaces:
'-------------------------------------------------------
 strRequest = "This is One string Number is Total of 32"
 response.write Len(replace(strRequest, " ", ""))
'-------------------------------------------------------
'#3 - count number of a particular character
'-------------------------------------------------------
'A Function with no loops - include in your page
'-------------------------------------------------------
 function charcount(strRequest, char)
   tmp = Split(Trim(strRequest), char)
   charcount = UBOUND(tmp)
 end function
'------------------------------------------------------
'Insert This where you want to count the character
'------------------------------------------------------
 strRequest = "The Cat Jumped Over the Wall. The End."
 response.write charcount(strRequest, ".")
'------------------------------------------------------
%>
Hope this helps :-)
Rory



© 1999-2004 Synop Pty Ltd