Reverse a String in VBScript


Using StrReverse Funtion

strText = "QTP Sreenu Blog"
strRevText = StrReverse(strText)
MsgBox strRevText

Without using StrReverse function (Using Len and Mid functions)

strText = "QTP Sreenu Blog"
iLen = Len(strText)
For i=iLento1Step -1
    strChar = Mid(strText,i,1) 'Retrieving each character from the end of string
    strRevText = strRevText & strChar
Next
MsgBox strRevText


Without using any built-in functions

strText = "QTP Sreenu Blog"
Set oRegExp = NewRegExp
oRegExp.Pattern = "\d|\D|\s|\S|\t|\w|\W"'This pattern matches with any character
oRegExp.Global = True
Set oItems = oRegExp.Execute(strText)
For i=0To oItems.Count-1
    strRevText = oItems(i) & strRevText
Next

For loop above can also be written as below
For i=oItems.Count-1To0Step -1
    strRevText = strRevText & oItems(i)
Next

MsgBox strRevText

No comments:

Post a Comment