Tricentis Tosca 16.0 Released on Feb-2023 ----- UFT has been upgraded from UFT 15.0.1 to UFT One 15.0.2, Beginning at November 2020.

Tuesday, 29 October 2019

Count the no of occurrences of a characters in a given string

Dim Str, ChkDup,Cnt,d
Set d = CreateObject("Scripting.Dictionary")
Str = "Community"
For i = 1 to Len(Str)
temp = 0
'Check if the key already exists
If d.Exists(mid(Str,i,1)) Then
    temp = d.Item(mid(Str,i,1)) 'Assign item to temp
    temp = CInt(temp) + 1 'Since temp is string convert to integer
    d.Item(mid(Str,i,1)) = temp 'Update the key
else
    d.Add mid(Str,i,1), 1 'if key doesn't exist, add it
End If
Next
sCharacters = d.Keys ' Get the keys.
iOccurrence = d.Items ' Get the items.
For x = 0 To d.Count-1 ' Iterate the array.
Print sCharacters(x) & " :" & iOccurrence(x)
Next


OutPut:-
C :1
o :1
m :2
u :1
n :1
i :1
t :1
y :1

Count the no of occurrences of string in a given String..?

inputstring = "This String has multiple statements and each statement has multiple occurrences"
Set o1 = new RegExp
o1.pattern = "statement"
o1.ignorecase = true
o1.global = true
Set r1 = o1.execute(inputstring)
msgbox r1.count


Out put:-
2

No comments:

Post a Comment

Note: only a member of this blog may post a comment.