Tuesday, May 11, 2010

How to reverse a string using vbscript

str="uday"
For i=len(str) to 1 step -1
strchar=mid(str,i,1)
resultstr=resultstr&strchar
Next
msgbox resultstr

or you can use the built-in function in vbscript
msgbox strreverse("uday")

4 comments:

淑娟 said...

thank for share, it is very important . ̄︿ ̄

J B C said...

'Here is how we can Reverse a String without using any functions like len & mid

Option Explicit
Dim yourstr,r,letter,result
yourstr="program"

Set r=new regexp
r.pattern="[a-z A-Z]"
r.global=true
set s=r.execute(yourstr)
For each letter in s
result= letter.value&result
Next
print result

Anem Udaya Kumar said...

Thanks for your post J B C.

But i wanted to know what that pattern means?

r.pattern="[a-z A-Z]"

Deep said...

This pattern means that it will reverse any string which contains range of letters from a to z and A to Z and also range of numericals from 0 to 9.
So, if you keep your pattern like "[a-z A-Z]" and suppose you give your string as "QUeR123y"; so your answer will be "yReUQ". It will not consider the numbers since you skipped them when you defined your pattern.
I hopr you got it.