Showing posts with label len. Show all posts
Showing posts with label len. Show all posts

Tuesday, 13 May 2014

QTP-UFT-How do you reverse a given String using VBS



How do you reverse a given String using VBS or QTP/UFT


Consider a variable 'a' to which we assign the input string. Input string in our case is "UFTQUESTIONS"


Approach 1
Use the StrReverse method

Script
a = "UFTQUESTIONS"
b= StrReverse(a)
msgbox b


Approach 2

Script
a="UFTQUESTIONS"
b=""
For i=0 to len(a)-1
    p=mid(a, len(a)-i, 1)
    b=b+p
Next
msgbox b

 
 
Output will be kept in variable 'b'
 

Saturday, 3 May 2014

QTP - UFT - Can you write a script to sort "uftquestions" as "efinoqssttuu"

One of the best method I would use for sorting is using object of "System.Collections.ArrayList"

Code:
Set o1 = Createobject("system.collections.ArrayList")
strInput = "UFTQuestions"
For k = 1 To len(strInput)
    o1.Add mid(strInput,k,1)
Next
o1.sort

For uj = 0 to o1.count-1
    strOutput = strOutput & o1(uj)
Next
msgbox strOutput

Output:- strOutput is "eFinoQsstTuU"

Happy reading,
Sreenu Babu

Sunday, 27 April 2014

UFT - QTP - How to get certain part of string using VBScript




 

 
 

We Can use the mid method
'output = Mid(InputString, StartPosition, NoofCharacterRequired)

Syntax
output = Mid("UFT is Great Fun", 5, 5)
Msgbox output

Alternate method

' Comments
'Extract a substring between a "Pre" substring and an "Post" substring.
'e.g.  Util_Text_SubStr("This is my string", "is ", "r")
' will return "is my st"

Function  Util_Text_SubStr(ByVal sString, ByVal sPre, ByVal sPost)
    Dim nStart, nEnd
    Util_Text_SubStr = ""
    If VarType(sString) <> vbString Then
        Exit Function
    End If
    nStart = instr(1,sString,sPre,vbTextCompare)
    If nStart = 0 Then
        Exit Function
    End If
    nStart = nStart + Len(sPre)
    If nStart > Len(sString) Then
        Exit Function
    End If
    If Len(sPost)>0 Then
        nEnd = instr(nStart,sString,sPost,vbTextCompare)
        If nEnd = 0 Then
            Exit Function
        End If
        nEnd = nEnd-1
    Else
        nEnd = Len(sString)
    End If
    If nEnd > nStart Then
        Util_Text_SubStr = mid(sString,nStart,(nEnd-nStart)+1)
    End If
End Function

Happy reading,
Sreenu Babu