Reverse a String with StrReverse() in Classic ASP

StrReverse() reverses the order of characters in a string. In Classic ASP, use it when you want to display a string backwards or compare text with its reversed version. Here is a complete VBScript example for an .asp file:

<%
Dim my_string
my_string = "Plus2net"
Response.Write StrReverse(my_string)
%>

Output: ten2sulP. The original variable stays unchanged unless you assign the reversed result back to it.

StrReverse() syntax and return value

StrReverse(expression)

expression is a string or string-valued variable. StrReverse() returns a new string, ordered from the last character to the first. It does not reorder words independently, and it is not an encryption function.

ExpressionResultExplanation
StrReverse("ABC")CBACharacters appear in reverse order
StrReverse("12345")54321Digits reverse like other characters
StrReverse("A B")B AThe space remains between the reversed letters
StrReverse("")Empty stringNo characters to reverse
StrReverse(Null)ErrorCheck nullable values first

Complete Classic ASP StrReverse() example

The original Plus2Net example prints a string, replaces the variable with its reversed value, and then prints the result. Both statements use Response.Write in Classic ASP.

<%
Dim my_string
my_string="Welcome to Plus2net.com"
Response.Write "Before String Reverse = " & my_string
my_string=StrReverse(my_string)
Response.Write "<br>After String Reversal = " & my_string
%>

Output

Before String Reverse = Welcome to Plus2net.com
After String Reversal = moc.ten2sulP ot emocleW

The period, spaces, and capitalization are preserved as characters, but all of their positions move. The change is visible because the reversed result is assigned back to my_string.

Reverse strings containing spaces, numbers and punctuation

StrReverse() does not remove whitespace or punctuation. It reverses the complete sequence. Compare these results:

<%
Response.Write StrReverse("ASP 2026") & "<br>"
Response.Write StrReverse("Hi! How are you?") & "<br>"
Response.Write StrReverse("A B C")
%>

Output

6202 PSA
?uoy era woH !iH
C B A

Normal HTML can visually collapse repeated spaces. To make every space visible, put the output between marker characters or show it in an HTML element that preserves whitespace.

Keep the original string and store the reversed string separately

Calling StrReverse() does not modify the input variable. Assign its return value to a new variable when you need both versions.

<%
Dim originalText, reversedText
originalText = "Classic ASP"
reversedText = StrReverse(originalText)
Response.Write "Original: " & originalText & "<br>"
Response.Write "Reversed: " & reversedText
%>
Original: Classic ASP
Reversed: PSA cissalC

Reverse a string twice

Applying the same function again returns the original order of characters. This is useful for demonstrating that reversal is a reversible text transformation, not data protection.

<%
Dim textValue, reversedOnce
textValue = "Welcome"
reversedOnce = StrReverse(textValue)
Response.Write reversedOnce & "<br>"
Response.Write StrReverse(reversedOnce)
%>
emocleW
Welcome

Check whether a word is a palindrome

A palindrome reads the same forwards and backwards. First use Trim() to remove outer spaces and LCase() to ignore letter case. Then compare the normalized string with its reverse.

<%
Dim word, normalized
word = "Madam"
normalized = LCase(Trim(word))
If normalized = StrReverse(normalized) Then
    Response.Write "Palindrome"
Else
    Response.Write "Not a palindrome"
End If
%>

Output: Palindrome.

This simple check ignores uppercase/lowercase differences and outer spaces only. It does not remove punctuation or spaces inside a sentence. For example, "Never odd or even" needs additional preprocessing before the whole phrase can be evaluated as a palindrome.

Reverse word order without reversing individual letters

StrReverse("Welcome to ASP") produces PSA ot emocleW. To get ASP to Welcome, use Split() and reverse the order of the array elements, then combine them with Join().

<%
Dim words, reversedWords, i, inputText
inputText = "Welcome to ASP"
words = Split(inputText, " ")
ReDim reversedWords(UBound(words))
For i = LBound(words) To UBound(words)
    reversedWords(UBound(words) - i) = words(i)
Next
Response.Write Join(reversedWords, " ")
%>

Output: ASP to Welcome. This example reverses space-separated words. Multiple consecutive spaces create empty array items, so normalize spacing first if exact word segmentation matters.

Reverse text submitted through a Classic ASP form

This standalone example shows how to read a visitor-entered value from Request.Form(), reverse it, and display the result safely. Save the example in a .asp file on an IIS server to run it.

<form method="post" action="">
  <label for="user_text">Enter text</label>
  <input type="text" id="user_text" name="user_text" maxlength="200">
  <button type="submit">Reverse text</button>
</form>
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
    Dim userText
    userText = Request.Form("user_text")
    If Not IsNull(userText) Then
        If Len(userText) > 0 Then
            Response.Write "<p>Reversed: " & _
                Server.HTMLEncode(StrReverse(userText)) & "</p>"
        Else
            Response.Write "<p>Please enter some text.</p>"
        End If
    End If
End If
%>

The output is passed through Server.HTMLEncode() because the text comes from a user. The maxlength attribute improves the browser experience, but server-side length limits and validation are still needed for production forms. Use HTTPS for data submitted to a live application.

How StrReverse() handles empty strings and Null

Reversing "" returns "". Passing Null directly causes a VBScript error, so check for it before calling the function.

<%
Dim dataValue
dataValue = Null
If IsNull(dataValue) Then
    Response.Write "No value to reverse"
Else
    Response.Write StrReverse(CStr(dataValue))
End If
%>

Output: No value to reverse. The IsNull() condition prevents a runtime error for nullable values returned from form processing or database records. In VBScript, an uninitialized variable and a Null value are different states; do not assume they behave identically.

For a non-null empty string, test the length before performing additional processing:

<%
Dim my_string
my_string = ""
If Len(my_string) = 0 Then
    Response.Write "The string is empty"
Else
    Response.Write StrReverse(my_string)
End If
%>

Reverse a string manually using Mid() and Len()

The built-in function is the simplest approach, but a loop helps explain the position calculations. Mid() starts counting at position 1, while Len() gives the last character position.

<%
Dim sourceText, backwardText, i
sourceText = "PLUS2NET"
backwardText = ""
For i = Len(sourceText) To 1 Step -1
    backwardText = backwardText & Mid(sourceText, i, 1)
Next
Response.Write backwardText
%>

Output: TEN2SULP. In regular code, prefer the shorter and clearer StrReverse(sourceText) expression unless you need to process individual characters while looping. See Mid() and other string extraction functions.

Common errors and troubleshooting

ProblemLikely reasonWhat to check
Runtime error with StrReverse(Null)Null is not a valid string argument.Use IsNull() before reversing.
Original variable looks unchangedThe result was not assigned back.Use value = StrReverse(value), or store it in a second variable.
Spaces or punctuation move unexpectedlyEvery character position is reversed.Use Split() / Join() to reorder whole words instead.
Text renders strangely when printedThe output includes HTML-sensitive characters.Use Server.HTMLEncode() on untrusted text.
ASP code prints as plain textThe code is not running in Classic ASP.Use an IIS server configured to execute .asp pages.

Frequently asked questions

Does StrReverse() change the original variable?

No. It returns a new string; assign the result back to the same variable to replace the previous value.

Can StrReverse() be used for palindrome checks?

Yes. Compare the normalized input with the output of StrReverse(). Decide which punctuation, spaces and letter-case differences to ignore.

Can StrReverse() reverse words without reversing their letters?

Not by itself. It reverses character order. Use Split() and Join() when you need to reverse the order of separate words.

What happens when the argument is an empty string or Null?

An empty string remains empty. Null raises an error; check it before the call. See the Microsoft StrReverse() reference for the documented behavior.

Is reversing text a secure way to hide a password?

No. String reversal is straightforward to undo. Do not use it to protect passwords, tokens or other sensitive values.

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer