Micro Focus QTP (UFT) Forums

Full Version: VB Script:number of times a character appears in a string with position
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Experts,
Please help me out ,
How do I count the number of times a character appears in a string with position .
My code given below working fine but i am not able to find position of the character.

Code:
sample="this, that, other, thing, too"
CharacterCount = Len(Sample) - Len(Replace(Sample, "i", ""))
WScript.Echo CharacterCount

Thanks in advance
Hi,

We can find the number of occurences in many ways, but as far as I know we have to use the regular loops to get the position numbers with a few modifications

Code:
Dim i, index, arr()
index = 0
For i=1 to len(string)
  If strcomp(Mid(string, i, 1), "[i]character to be searched[/i]", vbTextCompare) = 0 Then
    redim preserve arr(index)
    arr(index) = i
    index = index+1
  End If
Next

In the above code you can also replace strcomp with instr(1, string,"searchstring", compareType) to get the same result.

Cheers