Micro Focus QTP (UFT) Forums
VB Script:number of times a character appears in a string with position - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: VB Script:number of times a character appears in a string with position (/Thread-VB-Script-number-of-times-a-character-appears-in-a-string-with-position)



VB Script:number of times a character appears in a string with position - Jyobtech - 08-06-2013

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


RE: VB Script:number of times a character appears in a string with position - anil2u - 08-07-2013

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