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

Sunday, 1 December 2019

Reverse an array with reverse string value

arr = array("apple", "mango","orange")
str = join(arr,"$")     'str = apple$mango$orange
str = strreverse(str)  'str = egnaro$ognam$elppa 
arr = split(str,"$")    'arr = {egnaro,ognam,elppa} 

for i=lbound(arr) to ubound(arr)
      MsgBox arr(i)
next

Reverse an array

arr = array(5,4,3,2,1)
istart = lbound(arr)
iEnd = ubound(arr)

for i=istart to iEnd/2
    temp = arr(i)
    arr(i) = arr(iEnd - i) 
    arr(iEnd - i) = temp
Next

for i=istart to iEnd
      MsgBox arr(i)
Next

Reverse a string without using strrev function

str = "VB Script"
for i=len(str) to 1 step -1
     temp = temp & mid(str,i,1) 
next
str = temp
MsgBox temp

Verify a number exists in an array or not

intFindNumber = 5
arr = array(4,2,7,8,5)
for i=lbound(arr) to ubound(arr)
     if arr(i) = intFindNumber then
         Msgbox "Number Found"
         Exit For
     end if
Next

Convert a string into an array without using Split function

Dim arr()
strText = "Hello"

intlength = len(strText)
intUbound = intlength - 1
Redim arr(intUbound)

For i = 1 to len(strText)
   arr(i-1) = mid(strText,i,1)
Next

'Display Ubound of array and its elements

MsgBox ubound(arr)
for i = lbound(arr) to ubound(arr)
   MsgBox arr(i)
Next

Convert an array into string and vice versa.

Join function returns a String after joining substrings present in an array.

arr = array("H","e","l","l","o")
strText = join(arr,";")
MsgBox strText


Split function returns an Array after splitting a string with delimiter. e.g ";"
Note : Default delimiter is the space character.

newarr = split(strText,";")
arrUbound =  Ubound(newarr)
For i = 0 to arrUbound
     MsgBox newarr(i)
Next