Monday, 29 December 2014

VB Script to find the number of occurences of a char in a string

Method 1:
str="Hello world"
Set regex=new RegExp
regex.pattern="l"
regex.global=true
Set matches=regex.execute(str)
msgbox matches.count

Method 2:
arr=split(str,"l")
msgbox ubound(arr)

Method 3:
counter=0
For i=1 to len(str)
   char=mid(str,i,1)
   If char="l" Then
     counter=counter+1
   End If
Next

msgbox counter

No comments:

Post a Comment