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(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.
| Expression | Result | Explanation |
|---|---|---|
StrReverse("ABC") | CBA | Characters appear in reverse order |
StrReverse("12345") | 54321 | Digits reverse like other characters |
StrReverse("A B") | B A | The space remains between the reversed letters |
StrReverse("") | Empty string | No characters to reverse |
StrReverse(Null) | Error | Check nullable values first |
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
%>
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.
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")
%>
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.
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
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
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.
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.
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.
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
%>
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.
| Problem | Likely reason | What to check |
|---|---|---|
Runtime error with StrReverse(Null) | Null is not a valid string argument. | Use IsNull() before reversing. |
| Original variable looks unchanged | The result was not assigned back. | Use value = StrReverse(value), or store it in a second variable. |
| Spaces or punctuation move unexpectedly | Every character position is reversed. | Use Split() / Join() to reorder whole words instead. |
| Text renders strangely when printed | The output includes HTML-sensitive characters. | Use Server.HTMLEncode() on untrusted text. |
| ASP code prints as plain text | The code is not running in Classic ASP. | Use an IIS server configured to execute .asp pages. |
No. It returns a new string; assign the result back to the same variable to replace the previous value.
Yes. Compare the normalized input with the output of StrReverse(). Decide which punctuation, spaces and letter-case differences to ignore.
Not by itself. It reverses character order. Use Split() and Join() when you need to reverse the order of separate words.
An empty string remains empty. Null raises an error; check it before the call. See the Microsoft StrReverse() reference for the documented behavior.
No. String reversal is straightforward to undo. Do not use it to protect passwords, tokens or other sensitive values.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.