Micro Focus QTP (UFT) Forums
VB script Coding with strings. - 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 Coding with strings. (/Thread-VB-script-Coding-with-strings)



VB script Coding with strings. - Tejas Tikhe - 07-15-2012

Hello,

Can anyone please help me in following task.

There is paragraph below which is a string.

strpara="ABC is good at testing.ABC is working on PC.ABC is playing games."

write a code for:- Calculate the occurrences of word “ABC” in the string given above.

Thanks.


RE: VB script Coding with strings. - ravi.gajul - 07-15-2012

Try this,
Code:
strng="ABC is good at testing.ABC is working on PC.ABC is playing games."
Function RegExpTest(patrn, strng)
   Dim regEx, Match, Matches   ' Create variable.
   Set regEx = New RegExp   ' Create a regular expression.
   regEx.Pattern = patrn   ' Set pattern.
   regEx.IgnoreCase = True   ' Set case insensitivity.
   regEx.Global = True   ' Set global applicability.
   Set Matches = regEx.Execute(strng)   ' Execute search.
   For Each Match in Matches   ' Iterate Matches collection.
      RetStr = RetStr & "Match found at position "
      RetStr = RetStr & Match.FirstIndex & ". Match Value is '"
      RetStr = RetStr & Match.Value & "'." & vbCRLF
   Next
   RegExpTest = RetStr
   msgbox "No of occorences:  "&Matches.count
End Function
MsgBox(RegExpTest("abc.",strng))

Regards,
Ravi


RE: VB script Coding with strings. - souvikghosh_diatm - 08-17-2012

I don't think a function is required for this... its as simple as follows which calculate the occurrences as well....


Code:
strString = "ABC is good at testing.ABC is working on PC.ABC is playing games."

ArrstrString = Split (strString, "ABC")

strCount = Ubound(ArrstrString)

msgbox ("No of occurrences: "&strCount)



RE: VB script Coding with strings. - ravi.gajul - 08-18-2012

Good one...


RE: VB script Coding with strings. - DevilkID - 09-11-2012

Thanks for the script, it's very simple.

Smile