<!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 - right_dollar - String">
<title>right_dollar - 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> / right_dollar</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">
<h1 id="right-function">Right$ Function</h1>
<p>The <code>Right$</code> function in Visual Basic 6 returns a string containing a specified number of
characters from the right side (end) of a string. The dollar sign (<code>$</code>) suffix indicates
that this function always returns a <code>String</code> type, never a <code>Variant</code>.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Right$(string, length)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>string</code> - Required. String expression from which the rightmost characters are returned.
If <code>string</code> contains <code>Null</code>, <code>Null</code> is returned.</li>
<li><code>length</code> - Required. Numeric expression indicating how many characters to return. If 0,
a zero-length string ("") is returned. If greater than or equal to the number of characters
in <code>string</code>, the entire string is returned.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>String</code> containing the rightmost <code>length</code> characters of <code>string</code>.</p>
<h2 id="behavior-and-characteristics">Behavior and Characteristics</h2>
<h3 id="length-handling">Length Handling</h3>
<ul>
<li>If <code>length</code> = 0: Returns an empty string ("")</li>
<li>If <code>length</code> >= <code>Len(string)</code>: Returns the entire string</li>
<li>If <code>length</code> < 0: Generates a runtime error (Invalid procedure call or argument)</li>
<li>If <code>string</code> is empty (""): Returns an empty string regardless of <code>length</code></li>
</ul>
<h3 id="null-handling">Null Handling</h3>
<ul>
<li>If <code>string</code> contains <code>Null</code>: Returns <code>Null</code></li>
<li>If <code>length</code> is <code>Null</code>: Generates a runtime error (Invalid use of Null)</li>
</ul>
<h3 id="type-differences-right-vs-right">Type Differences: <code>Right$</code> vs <code>Right</code></h3>
<ul>
<li><code>Right$</code>: Always returns <code>String</code> type (never <code>Variant</code>)</li>
<li><code>Right</code>: Returns <code>Variant</code> (can propagate <code>Null</code> values)</li>
<li>Use <code>Right$</code> when you need guaranteed <code>String</code> return type</li>
<li>Use <code>Right</code> when working with potentially <code>Null</code> values</li>
</ul>
<h2 id="common-usage-patterns">Common Usage Patterns</h2>
<h3 id="1-extract-file-extension">1. Extract File Extension</h3>
<pre><code class="language-vbnet">Function GetExtension(fileName As String) As String
Dim dotPos As Integer
dotPos = InStrRev(fileName, ".")
If dotPos > 0 Then
GetExtension = Right$(fileName, Len(fileName) - dotPos)
Else
GetExtension = ""
End If
End Function
Dim ext As String
ext = GetExtension("document.txt") ' Returns "txt"</code></pre>
<h3 id="2-get-last-n-characters">2. Get Last N Characters</h3>
<pre><code class="language-vbnet">Dim text As String
Dim suffix As String
text = "Hello World"
suffix = Right$(text, 5) ' Returns "World"</code></pre>
<h3 id="3-extract-account-number-suffix">3. Extract Account Number Suffix</h3>
<pre><code class="language-vbnet">Function GetAccountSuffix(accountNum As String) As String
' Get last 4 digits of account number
GetAccountSuffix = Right$(accountNum, 4)
End Function
Dim lastFour As String
lastFour = GetAccountSuffix("1234567890") ' Returns "7890"</code></pre>
<h3 id="4-pad-string-to-fixed-width">4. Pad String to Fixed Width</h3>
<pre><code class="language-vbnet">Function PadLeft(text As String, width As Integer) As String
Dim padded As String
padded = Space(width) & text
PadLeft = Right$(padded, width)
End Function
Dim result As String
result = PadLeft("42", 5) ' Returns " 42"</code></pre>
<h3 id="5-extract-trailing-digits">5. Extract Trailing Digits</h3>
<pre><code class="language-vbnet">Function GetTrailingNumber(text As String) As String
Dim i As Integer
Dim numChars As Integer
For i = Len(text) To 1 Step -1
If Not IsNumeric(Mid$(text, i, 1)) Then Exit For
numChars = numChars + 1
Next i
If numChars > 0 Then
GetTrailingNumber = Right$(text, numChars)
Else
GetTrailingNumber = ""
End If
End Function
Dim num As String
num = GetTrailingNumber("Item123") ' Returns "123"</code></pre>
<h3 id="6-time-component-extraction">6. Time Component Extraction</h3>
<pre><code class="language-vbnet">Function GetSeconds(timeStr As String) As String
' Extract seconds from "HH:MM:SS" format
GetSeconds = Right$(timeStr, 2)
End Function
Dim secs As String
secs = GetSeconds("14:30:45") ' Returns "45"</code></pre>
<h3 id="7-validate-string-suffix">7. Validate String Suffix</h3>
<pre><code class="language-vbnet">Function HasExtension(fileName As String, ext As String) As Boolean
Dim fileExt As String
fileExt = Right$(fileName, Len(ext))
HasExtension = (UCase$(fileExt) = UCase$(ext))
End Function
If HasExtension("report.pdf", ".pdf") Then
Debug.Print "PDF file detected"
End If</code></pre>
<h3 id="8-extract-domain-from-email">8. Extract Domain from Email</h3>
<pre><code class="language-vbnet">Function GetEmailDomain(email As String) As String
Dim atPos As Integer
atPos = InStr(email, "@")
If atPos > 0 Then
GetEmailDomain = Right$(email, Len(email) - atPos)
Else
GetEmailDomain = ""
End If
End Function
Dim domain As String
domain = GetEmailDomain("user@example.com") ' Returns "example.com"</code></pre>
<h3 id="9-format-currency-display">9. Format Currency Display</h3>
<pre><code class="language-vbnet">Function FormatAmount(amount As String) As String
' Align decimal values
Dim formatted As String
formatted = Space(15) & amount
FormatAmount = Right$(formatted, 15)
End Function</code></pre>
<h3 id="10-extract-path-component">10. Extract Path Component</h3>
<pre><code class="language-vbnet">Function GetFileName(fullPath As String) As String
Dim slashPos As Integer
slashPos = InStrRev(fullPath, "\")
If slashPos > 0 Then
GetFileName = Right$(fullPath, Len(fullPath) - slashPos)
Else
GetFileName = fullPath
End If
End Function
Dim fileName As String
fileName = GetFileName("C:\Documents\report.txt") ' Returns "report.txt"</code></pre>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Right()</code> - Returns a <code>Variant</code> containing the rightmost characters (can handle <code>Null</code>)</li>
<li><code>Left$()</code> - Returns a specified number of characters from the left side of a string</li>
<li><code>Mid$()</code> - Returns a specified number of characters from any position in a string</li>
<li><code>Len()</code> - Returns the number of characters in a string</li>
<li><code>InStrRev()</code> - Finds the position of a substring searching from the end</li>
<li><code>Trim$()</code> - Removes leading and trailing spaces from a string</li>
<li><code>LTrim$()</code> - Removes leading spaces from a string</li>
<li><code>RTrim$()</code> - Removes trailing spaces from a string</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="when-to-use-right-vs-right">When to Use <code>Right$</code> vs <code>Right</code></h3>
<pre><code class="language-vbnet">' Use Right$ when you need a String
Dim fileName As String
fileName = Right$(fullPath, 10) ' Type-safe, always returns String
' Use Right when working with Variants or Null values
Dim result As Variant
result = Right(variantValue, 5) ' Can propagate Null</code></pre>
<h3 id="validate-length-parameter">Validate Length Parameter</h3>
<pre><code class="language-vbnet">Function SafeRight(text As String, length As Integer) As String
If length < 0 Then
SafeRight = ""
ElseIf length >= Len(text) Then
SafeRight = text
Else
SafeRight = Right$(text, length)
End If
End Function</code></pre>
<h3 id="check-for-empty-strings">Check for Empty Strings</h3>
<pre><code class="language-vbnet">If Len(text) > 0 Then
suffix = Right$(text, 3)
Else
suffix = ""
End If</code></pre>
<h3 id="use-with-instrrev-for-parsing">Use with <code>InStrRev</code> for Parsing</h3>
<pre><code class="language-vbnet">' Find last occurrence and extract everything after it
Dim pos As Integer
pos = InStrRev(fullPath, "\")
If pos > 0 Then
fileName = Right$(fullPath, Len(fullPath) - pos)
End If</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Right$</code> is very efficient for small to moderate length strings</li>
<li>For very large strings, consider if you really need to extract characters</li>
<li>Using <code>Right$</code> in tight loops with large strings may impact performance</li>
<li>Consider caching the length if calling <code>Len()</code> repeatedly</li>
</ul>
<pre><code class="language-vbnet">' Less efficient
For i = 1 To 1000
result = Right$(largeString, Len(largeString) - 10)
Next i
' More efficient
Dim strLen As Long
strLen = Len(largeString)
For i = 1 To 1000
result = Right$(largeString, strLen - 10)
Next i</code></pre>
<h2 id="common-pitfalls">Common Pitfalls</h2>
<h3 id="1-negative-length-values">1. Negative Length Values</h3>
<pre><code class="language-vbnet">' Runtime error: Invalid procedure call or argument
text = Right$("Hello", -1) ' ERROR!
' Validate first
If length >= 0 Then
text = Right$(source, length)
End If</code></pre>
<h3 id="2-off-by-one-errors">2. Off-by-One Errors</h3>
<pre><code class="language-vbnet">' Common mistake: forgetting to account for delimiter position
Dim pos As Integer
pos = InStrRev(path, "\")
' Wrong: includes the backslash
fileName = Right$(path, pos)
' Correct: excludes the backslash
fileName = Right$(path, Len(path) - pos)</code></pre>
<h3 id="3-not-checking-string-length">3. Not Checking String Length</h3>
<pre><code class="language-vbnet">' Potential issue: what if text is shorter than 10 characters?
suffix = Right$(text, 10) ' Returns entire string if text.Length < 10
' Better: check first
If Len(text) >= 10 Then
suffix = Right$(text, 10)
Else
' Handle short string case
suffix = text
End If</code></pre>
<h3 id="4-assuming-fixed-positions">4. Assuming Fixed Positions</h3>
<pre><code class="language-vbnet">' Fragile: assumes extension is always 3 characters
ext = Right$(fileName, 3) ' Fails for ".html", ".jpeg"
' Better: find the dot
Dim dotPos As Integer
dotPos = InStrRev(fileName, ".")
If dotPos > 0 Then
ext = Right$(fileName, Len(fileName) - dotPos)
End If</code></pre>
<h3 id="5-null-value-handling">5. Null Value Handling</h3>
<pre><code class="language-vbnet">' Right$ with Null causes runtime error
Dim result As String
result = Right$(nullValue, 5) ' ERROR if nullValue is Null
' Protect against Null
If Not IsNull(value) Then
result = Right$(value, 5)
Else
result = ""
End If</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Cannot handle <code>Null</code> values (use <code>Right</code> variant function instead)</li>
<li>No built-in trimming of whitespace (combine with <code>RTrim$</code> if needed)</li>
<li>Negative <code>length</code> values cause runtime errors</li>
<li>Works with characters, not bytes (use <code>RightB$</code> for byte-level operations)</li>
<li>No Unicode-specific version (VB6 uses UCS-2 internally)</li>
<li>Cannot extract from right based on delimiter (must calculate length manually)</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>