Saturday, 21 December 2019

Find the number of occurrence of a character or string in a given string



First Method : Here, we are updating the starting point of search after every iteration 

strText = "HelloHowareYou"
strFindChar = "u"
Counter = 0
iIndex = 1
Do While instr(iIndex,strText,strFindChar)   
     counter = counter + 1
     iIndex = instr(iIndex,strText,strFindChar)
     iIndex = iIndex + 1     
Loop
MsgBox  Counter

Second Method : 
strText = "HelloHowareYou"
strFindChar = "u"
Myarr = split(strTextstrFindChar)    'Splitting the string with strFindChar as delimiter
intNoOfOcc = ubound(Myarr)
MsgBox intNoOfOcc

No comments:

Post a Comment