Various ways to Reverse String in UFT/QTP using Vbscripting

Scenario: We need to reverse a string without using the Vb-scripting inbuilt function strReverse.

Solution 1: Using the “Mid” function to reverse the input string.

Example Code:

sRev = "UFTHELP, Ur First Testing Help"
Msgbox fnReverse(sRev)

Public Function fnReverse(sRev) 
    for i = 1 to len(sRev) 
        sNew = mid(sRev,i,1) & sNew 
    next
    fnReverse = sNew
End Function

Solution 2: Using the Split function and then combining the string in reverse order. This technique is recommended when we want our word order to be reversed rather than each character.

Example Code:

sRev = "UFTHELP, Ur First Testing Help"
Msgbox fnReverse(sRev)

Public Function fnReverse(sRev)
'Using Split function
arr = split(sRev, " ")
for i = Ubound(arr) to 0 Step -1
sNew = sNew & " " & arr(i)
next
fnReverse = sNew
End Function
Was this article helpful?
YesNo

Similar Posts