<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - strreverse - String">
<title>strreverse - String - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/string/index.html">String</a> / strreverse</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<p>VB6 StrReverse Function
The <code>StrReverse</code> function returns a string in which the character order of a specified string is reversed.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">StrReverse(expression)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>expression</code>: Required. String expression whose characters are to be reversed. If <code>expression</code> is a zero-length string (""), a zero-length string is returned.</li>
</ul>
<h2 id="returns">Returns</h2>
<p>Returns a <code>String</code> with the characters in reverse order.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>StrReverse</code> function reverses the order of characters in a string:
- <strong>Character-by-character reversal</strong>: Reverses individual characters, not words
- <strong>Unicode support</strong>: Works correctly with Unicode characters
- <strong>Empty string handling</strong>: Returns empty string if input is empty
- <strong>Null handling</strong>: Returns <code>Null</code> if <code>expression</code> is <code>Null</code>
- <strong>Preserves spaces</strong>: Spaces are treated like any other character and reversed
- <strong>Case preserved</strong>: Original case of characters is maintained
- <strong>Single pass</strong>: Efficient single-pass algorithm
- <strong>VB6/VBA only</strong>: Available in VB6 and VBA, not in <code>VBScript</code></p>
<h3 id="common-use-cases">Common Use Cases</h3>
<ul>
<li>Reversing strings for display or analysis</li>
<li>Palindrome checking (compare string with its reverse)</li>
<li>Text transformations and puzzles</li>
<li>Data obfuscation (simple, not secure)</li>
<li>Mirror text effects</li>
<li>String manipulation algorithms</li>
</ul>
<h3 id="comparison-with-manual-reversal">Comparison with Manual Reversal</h3>
<p><code>StrReverse</code> is more efficient than manually reversing with loops:</p>
<pre><code class="language-vbnet">' Using StrReverse (preferred)
reversed = StrReverse(original)
' Manual reversal (slower)
For i = Len(original) To 1 Step -1
reversed = reversed & Mid$(original, i, 1)
Next i</code></pre>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Palindrome Detection</strong>: Check if a string reads the same forwards and backwards</li>
<li><strong>Text Effects</strong>: Create mirror or reversed text displays</li>
<li><strong>String Analysis</strong>: Analyze patterns in reversed strings</li>
<li><strong>Data Transformation</strong>: Transform data for specific algorithms</li>
<li><strong>Puzzles and Games</strong>: Implement word games and puzzles</li>
<li><strong>File Processing</strong>: Process files that store data in reverse order</li>
<li><strong>Encoding</strong>: Simple (non-cryptographic) string obfuscation</li>
<li><strong>Testing</strong>: Generate test data with predictable patterns</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<h3 id="example-1-basic-string-reversal">Example 1: Basic String Reversal</h3>
<pre><code class="language-vbnet">Dim original As String
Dim reversed As String
original = "Hello"
reversed = StrReverse(original) ' "olleH"
original = "VB6"
reversed = StrReverse(original) ' "6BV"
original = "12345"
reversed = StrReverse(original) ' "54321"</code></pre>
<h3 id="example-2-palindrome-check">Example 2: Palindrome Check</h3>
<pre><code class="language-vbnet">Function IsPalindrome(text As String) As Boolean
Dim normalized As String
' Remove spaces and convert to lowercase for comparison
normalized = Replace(LCase$(text), " ", "")
' Compare with reversed version
IsPalindrome = (normalized = StrReverse(normalized))
End Function
' Examples:
' IsPalindrome("racecar") = True
' IsPalindrome("A man a plan a canal Panama") = True (after normalization)
' IsPalindrome("hello") = False</code></pre>
<h3 id="example-3-reverse-words-in-sentence">Example 3: Reverse Words in Sentence</h3>
<pre><code class="language-vbnet">Function ReverseWords(sentence As String) As String
Dim words() As String
Dim i As Integer
Dim result As String
words = Split(sentence, " ")
result = ""
For i = UBound(words) To LBound(words) Step -1
If i < UBound(words) Then result = result & " "
result = result & words(i)
Next i
ReverseWords = result
End Function
' Example: "Hello World" becomes "World Hello"</code></pre>
<h3 id="example-4-simple-obfuscation">Example 4: Simple Obfuscation</h3>
<pre><code class="language-vbnet">Function ObfuscateString(text As String) As String
' Simple, non-secure obfuscation
ObfuscateString = StrReverse(text)
End Function
Function DeobfuscateString(text As String) As String
' Reverse the obfuscation
DeobfuscateString = StrReverse(text)
End Function</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="pattern-1-check-palindrome-case-insensitive">Pattern 1: Check Palindrome (Case-Insensitive)</h3>
<pre><code class="language-vbnet">Function IsPalindromeIgnoreCase(text As String) As Boolean
Dim lower As String
lower = LCase$(text)
IsPalindromeIgnoreCase = (lower = StrReverse(lower))
End Function</code></pre>
<h3 id="pattern-2-reverse-each-word">Pattern 2: Reverse Each Word</h3>
<pre><code class="language-vbnet">Function ReverseEachWord(sentence As String) As String
Dim words() As String
Dim i As Integer
words = Split(sentence, " ")
For i = LBound(words) To UBound(words)
words(i) = StrReverse(words(i))
Next i
ReverseEachWord = Join(words, " ")
End Function</code></pre>
<h3 id="pattern-3-get-last-n-characters-efficiently">Pattern 3: Get Last N Characters Efficiently</h3>
<pre><code class="language-vbnet">Function GetLastNChars(text As String, n As Integer) As String
Dim reversed As String
reversed = StrReverse(text)
GetLastNChars = StrReverse(Left$(reversed, n))
End Function</code></pre>
<h3 id="pattern-4-check-if-strings-are-reverses">Pattern 4: Check If Strings Are Reverses</h3>
<pre><code class="language-vbnet">Function AreReverses(str1 As String, str2 As String) As Boolean
AreReverses = (str1 = StrReverse(str2))
End Function</code></pre>
<h3 id="pattern-5-reverse-file-extension">Pattern 5: Reverse File Extension</h3>
<pre><code class="language-vbnet">Function ReverseExtension(filename As String) As String
Dim dotPos As Integer
Dim name As String
Dim ext As String
dotPos = InStrRev(filename, ".")
If dotPos > 0 Then
name = Left$(filename, dotPos - 1)
ext = Mid$(filename, dotPos + 1)
ReverseExtension = name & "." & StrReverse(ext)
Else
ReverseExtension = filename
End If
End Function</code></pre>
<h3 id="pattern-6-mirror-text-display">Pattern 6: Mirror Text Display</h3>
<pre><code class="language-vbnet">Function CreateMirrorText(text As String) As String
CreateMirrorText = text & " | " & StrReverse(text)
End Function</code></pre>
<h3 id="pattern-7-reverse-and-uppercase">Pattern 7: Reverse and Uppercase</h3>
<pre><code class="language-vbnet">Function ReverseAndUpper(text As String) As String
ReverseAndUpper = UCase$(StrReverse(text))
End Function</code></pre>
<h3 id="pattern-8-find-reverse-match-in-array">Pattern 8: Find Reverse Match in Array</h3>
<pre><code class="language-vbnet">Function FindReverseMatch(arr() As String, searchValue As String) As Integer
Dim i As Integer
Dim reversed As String
reversed = StrReverse(searchValue)
For i = LBound(arr) To UBound(arr)
If arr(i) = reversed Then
FindReverseMatch = i
Exit Function
End If
Next i
FindReverseMatch = -1
End Function</code></pre>
<h3 id="pattern-9-reverse-between-delimiters">Pattern 9: Reverse Between Delimiters</h3>
<pre><code class="language-vbnet">Function ReverseBetween(text As String, startDelim As String, endDelim As String) As String
Dim startPos As Integer
Dim endPos As Integer
Dim middle As String
startPos = InStr(text, startDelim)
endPos = InStr(startPos + Len(startDelim), text, endDelim)
If startPos > 0 And endPos > startPos Then
middle = Mid$(text, startPos + Len(startDelim), endPos - startPos - Len(startDelim))
ReverseBetween = Left$(text, startPos + Len(startDelim) - 1) & _
StrReverse(middle) & _
Mid$(text, endPos)
Else
ReverseBetween = text
End If
End Function</code></pre>
<h3 id="pattern-10-alternate-characters-reversed">Pattern 10: Alternate Characters Reversed</h3>
<pre><code class="language-vbnet">Function AlternateReverse(text As String) As String
Dim i As Integer
Dim result As String
Dim reversed As String
reversed = StrReverse(text)
result = ""
For i = 1 To Len(text)
If i Mod 2 = 1 Then
result = result & Mid$(text, i, 1)
Else
result = result & Mid$(reversed, i, 1)
End If
Next i
AlternateReverse = result
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="example-1-palindrome-checker-class">Example 1: Palindrome Checker Class</h3>
<pre><code class="language-vbnet">' Class: PalindromeChecker
' Checks various types of palindromes
Option Explicit
Private m_IgnoreCase As Boolean
Private m_IgnoreSpaces As Boolean
Private m_IgnorePunctuation As Boolean
Public Sub Initialize(Optional ignoreCase As Boolean = True, _
Optional ignoreSpaces As Boolean = True, _
Optional ignorePunctuation As Boolean = False)
m_IgnoreCase = ignoreCase
m_IgnoreSpaces = ignoreSpaces
m_IgnorePunctuation = ignorePunctuation
End Sub
Public Function IsPalindrome(text As String) As Boolean
Dim normalized As String
normalized = NormalizeText(text)
IsPalindrome = (normalized = StrReverse(normalized))
End Function
Public Function GetLongestPalindromeSubstring(text As String) As String
Dim i As Integer
Dim j As Integer
Dim substring As String
Dim longest As String
longest = ""
For i = 1 To Len(text)
For j = i To Len(text)
substring = Mid$(text, i, j - i + 1)
If IsPalindrome(substring) And Len(substring) > Len(longest) Then
longest = substring
End If
Next j
Next i
GetLongestPalindromeSubstring = longest
End Function
Private Function NormalizeText(text As String) As String
Dim result As String
result = text
If m_IgnoreCase Then
result = LCase$(result)
End If
If m_IgnoreSpaces Then
result = Replace(result, " ", "")
End If
If m_IgnorePunctuation Then
result = RemovePunctuation(result)
End If
NormalizeText = result
End Function
Private Function RemovePunctuation(text As String) As String
Dim i As Integer
Dim char As String
Dim result As String
result = ""
For i = 1 To Len(text)
char = Mid$(text, i, 1)
If (char >= "A" And char <= "Z") Or (char >= "a" And char <= "z") Or _
(char >= "0" And char <= "9") Then
result = result & char
End If
Next i
RemovePunctuation = result
End Function</code></pre>
<h3 id="example-2-string-reversal-utilities">Example 2: String Reversal Utilities</h3>
<pre><code class="language-vbnet">' Module: StringReversalUtils
' Utilities for reversing strings in various ways
Option Explicit
Public Function ReverseString(text As String) As String
ReverseString = StrReverse(text)
End Function
Public Function ReverseWords(sentence As String) As String
Dim words() As String
Dim i As Integer
Dim result As String
words = Split(sentence, " ")
result = ""
For i = UBound(words) To LBound(words) Step -1
If Len(result) > 0 Then result = result & " "
result = result & words(i)
Next i
ReverseWords = result
End Function
Public Function ReverseEachWord(sentence As String) As String
Dim words() As String
Dim i As Integer
words = Split(sentence, " ")
For i = LBound(words) To UBound(words)
words(i) = StrReverse(words(i))
Next i
ReverseEachWord = Join(words, " ")
End Function
Public Function ReverseLines(text As String) As String
Dim lines() As String
Dim i As Integer
Dim result As String
lines = Split(text, vbCrLf)
result = ""
For i = UBound(lines) To LBound(lines) Step -1
If Len(result) > 0 Then result = result & vbCrLf
result = result & lines(i)
Next i
ReverseLines = result
End Function
Public Function ReverseArray(arr() As String) As String()
Dim result() As String
Dim i As Integer
Dim j As Integer
ReDim result(LBound(arr) To UBound(arr))
j = LBound(arr)
For i = UBound(arr) To LBound(arr) Step -1
result(j) = arr(i)
j = j + 1
Next i
ReverseArray = result
End Function</code></pre>
<h3 id="example-3-text-transformer-class">Example 3: Text Transformer Class</h3>
<pre><code class="language-vbnet">' Class: TextTransformer
' Performs various text transformations including reversal
Option Explicit
Public Function Transform(text As String, transformType As String) As String
Select Case UCase$(transformType)
Case "REVERSE"
Transform = StrReverse(text)
Case "REVERSE_WORDS"
Transform = ReverseWords(text)
Case "REVERSE_EACH_WORD"
Transform = ReverseEachWord(text)
Case "MIRROR"
Transform = text & " " & StrReverse(text)
Case "PALINDROME"
Transform = text & StrReverse(text)
Case Else
Transform = text
End Select
End Function
Private Function ReverseWords(sentence As String) As String
Dim words() As String
Dim i As Integer
Dim result As String
words = Split(sentence, " ")
result = ""
For i = UBound(words) To LBound(words) Step -1
If Len(result) > 0 Then result = result & " "
result = result & words(i)
Next i
ReverseWords = result
End Function
Private Function ReverseEachWord(sentence As String) As String
Dim words() As String
Dim i As Integer
words = Split(sentence, " ")
For i = LBound(words) To UBound(words)
words(i) = StrReverse(words(i))
Next i
ReverseEachWord = Join(words, " ")
End Function
Public Function CreatePalindrome(text As String) As String
CreatePalindrome = text & StrReverse(text)
End Function
Public Function IsPalindrome(text As String) As Boolean
IsPalindrome = (text = StrReverse(text))
End Function</code></pre>
<h3 id="example-4-string-analyzer-module">Example 4: String Analyzer Module</h3>
<pre><code class="language-vbnet">' Module: StringAnalyzer
' Analyzes strings using reversal techniques
Option Explicit
Public Function ContainsPalindrome(text As String, minLength As Integer) As Boolean
Dim i As Integer
Dim j As Integer
Dim substring As String
For i = 1 To Len(text) - minLength + 1
For j = minLength To Len(text) - i + 1
substring = Mid$(text, i, j)
If substring = StrReverse(substring) Then
ContainsPalindrome = True
Exit Function
End If
Next j
Next i
ContainsPalindrome = False
End Function
Public Function FindAllPalindromes(text As String, minLength As Integer) As Collection
Dim palindromes As New Collection
Dim i As Integer
Dim j As Integer
Dim substring As String
For i = 1 To Len(text)
For j = minLength To Len(text) - i + 1
substring = Mid$(text, i, j)
If substring = StrReverse(substring) Then
On Error Resume Next
palindromes.Add substring, substring
On Error GoTo 0
End If
Next j
Next i
Set FindAllPalindromes = palindromes
End Function
Public Function GetSymmetryScore(text As String) As Double
' Calculate how symmetric a string is (0-100%)
Dim matches As Integer
Dim i As Integer
Dim len As Integer
len = Len(text)
If len = 0 Then
GetSymmetryScore = 0
Exit Function
End If
matches = 0
For i = 1 To len \ 2
If Mid$(text, i, 1) = Mid$(text, len - i + 1, 1) Then
matches = matches + 1
End If
Next i
GetSymmetryScore = (matches / (len \ 2)) * 100
End Function
Public Function IsAnagram(str1 As String, str2 As String) As Boolean
' Not directly using StrReverse, but useful utility
Dim sorted1 As String
Dim sorted2 As String
sorted1 = SortString(LCase$(str1))
sorted2 = SortString(LCase$(str2))
IsAnagram = (sorted1 = sorted2)
End Function
Private Function SortString(text As String) As String
' Simple bubble sort for demonstration
Dim chars() As String
Dim i As Integer
Dim j As Integer
Dim temp As String
ReDim chars(1 To Len(text))
For i = 1 To Len(text)
chars(i) = Mid$(text, i, 1)
Next i
For i = 1 To UBound(chars) - 1
For j = i + 1 To UBound(chars)
If chars(i) > chars(j) Then
temp = chars(i)
chars(i) = chars(j)
chars(j) = temp
End If
Next j
Next i
SortString = Join(chars, "")
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p>The <code>StrReverse</code> function typically does not raise errors under normal circumstances:
- Returns empty string if input is empty string
- Returns <code>Null</code> if input is <code>Null</code> (not an error)
- <strong>Error 13 (Type mismatch)</strong>: If <code>expression</code> cannot be converted to a string</p>
<h2 id="performance-notes">Performance Notes</h2>
<ul>
<li>Very fast and efficient (optimized native function)</li>
<li>Much faster than manual character-by-character reversal in VB6</li>
<li>Performance is O(n) where n is string length</li>
<li>No significant overhead for typical string lengths</li>
<li>For very large strings (megabytes), consider memory constraints</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Use <code>StrReverse</code></strong> instead of manual loops for reversing strings (faster and cleaner)</li>
<li><strong>Handle Null values</strong> explicitly when working with Variant types</li>
<li><strong>Normalize before palindrome checks</strong> (remove spaces, convert case)</li>
<li><strong>Don't use for security</strong> - <code>StrReverse</code> is not encryption, only obfuscation</li>
<li><strong>Cache reversed strings</strong> if used multiple times in comparisons</li>
<li><strong>Combine with other functions</strong> like <code>LCase$</code>, <code>Trim$</code> for text processing</li>
<li><strong>Test edge cases</strong> like empty strings and single-character strings</li>
<li><strong>Document intent</strong> when using <code>StrReverse</code> in non-obvious ways</li>
<li><strong>Consider alternatives</strong> for word-level reversal (Split/Join approach)</li>
<li><strong>Use for validation</strong> like palindrome checking or symmetry analysis</li>
</ol>
<h2 id="comparison-table">Comparison Table</h2>
<table>
<thead>
<tr>
<th>Approach</th>
<th>Code</th>
<th>Speed</th>
<th>Clarity</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>StrReverse</code></td>
<td><code>StrReverse(s)</code></td>
<td>Fast</td>
<td>Excellent</td>
</tr>
<tr>
<td>Manual loop</td>
<td><code>For i = Len(s) To 1 Step -1...</code></td>
<td>Slow</td>
<td>Poor</td>
</tr>
<tr>
<td>Recursion</td>
<td><code>ReverseRecursive(s)</code></td>
<td>Very slow</td>
<td>Poor</td>
</tr>
</tbody>
</table>
<h2 id="platform-notes">Platform Notes</h2>
<ul>
<li>Available in VB6 and VBA</li>
<li><strong>Not available in <code>VBScript</code></strong> (must implement manually)</li>
<li>Works correctly with Unicode characters</li>
<li>Behavior consistent across VB6 and VBA</li>
<li>No locale-specific behavior</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Cannot reverse only part of a string (use <code>Mid$</code> to extract first)</li>
<li>Cannot reverse words rather than characters (use Split/Join)</li>
<li>Not available in <code>VBScript</code></li>
<li>Returns <code>Null</code> for <code>Null</code> input (may be unexpected)</li>
<li>No option to reverse specific character ranges</li>
<li>Cannot specify custom reversal rules</li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to String</a> |
<a href="../index.html">View all functions</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>