vb6parse 1.0.0

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
<!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 - String">
    <title>right - 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</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>Returns a String containing a specified number of characters from the right side of a string.</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.</li>
<li><code>length</code> - Required. Long 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 specified number of characters from the right side of the string.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Right</code> function extracts a substring from the end of a string. It is commonly used for:
- Extracting file extensions
- Getting the last N characters of a string
- Parsing fixed-width data from the right
- Removing prefixes or extracting suffixes
<strong>Important Notes</strong>:
- If <code>length</code> is 0, an empty string ("") is returned
- If <code>length</code> &gt;= Len(string), the entire string is returned
- If <code>string</code> is Null, Right returns Null
- If <code>length</code> is negative, a runtime error occurs (Error 5: Invalid procedure call or argument)
- The function is 1-based (counts from position 1, not 0)
<strong>Behavior with Different Length Values</strong>:</p>
<table>
<thead>
<tr>
<th>Length Value</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>Empty string ("")</td>
</tr>
<tr>
<td>1 to Len(string)</td>
<td>Rightmost N characters</td>
</tr>
<tr>
<td>&gt; Len(string)</td>
<td>Entire string</td>
</tr>
<tr>
<td>Negative</td>
<td>Runtime error (Error 5)</td>
</tr>
</tbody>
</table>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>File Extensions</strong>: Extract file extension from filename</li>
<li><strong>Data Parsing</strong>: Extract rightmost fields from fixed-width data</li>
<li><strong>String Validation</strong>: Check string endings/suffixes</li>
<li><strong>Number Formatting</strong>: Get last digits of numbers</li>
<li><strong>Path Manipulation</strong>: Extract filename from path</li>
<li><strong>Text Processing</strong>: Remove prefixes, keep suffixes</li>
<li><strong>Data Extraction</strong>: Get trailing characters from codes/IDs</li>
<li><strong>String Truncation</strong>: Keep only the rightmost portion</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<h3 id="example-1-extract-file-extension">Example 1: Extract File Extension</h3>
<pre><code class="language-vbnet">Dim filename As String
Dim extension As String
filename = &quot;document.txt&quot;
extension = Right(filename, 4)  &#x27; Returns &quot;.txt&quot;</code></pre>
<h3 id="example-2-get-last-n-characters">Example 2: Get Last N Characters</h3>
<pre><code class="language-vbnet">Dim accountNumber As String
Dim lastFour As String
accountNumber = &quot;1234567890&quot;
lastFour = Right(accountNumber, 4)  &#x27; Returns &quot;7890&quot;</code></pre>
<h3 id="example-3-check-string-ending">Example 3: Check String Ending</h3>
<pre><code class="language-vbnet">Dim filename As String
filename = &quot;report.pdf&quot;
If Right(filename, 4) = &quot;.pdf&quot; Then
    MsgBox &quot;This is a PDF file&quot;
End If</code></pre>
<h3 id="example-4-extract-from-fixed-width-data">Example 4: Extract from Fixed-Width Data</h3>
<pre><code class="language-vbnet">Dim record As String
Dim zipCode As String
record = &quot;John Doe          New York     10001&quot;
zipCode = Right(record, 5)  &#x27; Returns &quot;10001&quot;</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="pattern-1-getfileextension">Pattern 1: <code>GetFileExtension</code></h3>
<pre><code class="language-vbnet">Function GetFileExtension(filename As String) As String
    &#x27; Extract file extension including the dot
    Dim dotPos As Integer
    dotPos = InStrRev(filename, &quot;.&quot;)
    If dotPos &gt; 0 Then
        GetFileExtension = Right(filename, Len(filename) - dotPos + 1)
    Else
        GetFileExtension = &quot;&quot;
    End If
End Function</code></pre>
<h3 id="pattern-2-getlastnchars">Pattern 2: <code>GetLastNChars</code></h3>
<pre><code class="language-vbnet">Function GetLastNChars(text As String, n As Long) As String
    &#x27; Safely get last N characters (won&#x27;t error if string is too short)
    If n &lt;= 0 Then
        GetLastNChars = &quot;&quot;
    ElseIf n &gt;= Len(text) Then
        GetLastNChars = text
    Else
        GetLastNChars = Right(text, n)
    End If
End Function</code></pre>
<h3 id="pattern-3-endswithstring">Pattern 3: <code>EndsWithString</code></h3>
<pre><code class="language-vbnet">Function EndsWithString(text As String, suffix As String, _
                       Optional caseSensitive As Boolean = True) As Boolean
    &#x27; Check if string ends with a specific suffix
    Dim textEnd As String
    Dim suffixLen As Long
    suffixLen = Len(suffix)
    If suffixLen &gt; Len(text) Then
        EndsWithString = False
        Exit Function
    End If
    textEnd = Right(text, suffixLen)
    If caseSensitive Then
        EndsWithString = (textEnd = suffix)
    Else
        EndsWithString = (UCase(textEnd) = UCase(suffix))
    End If
End Function</code></pre>
<h3 id="pattern-4-removeprefix">Pattern 4: <code>RemovePrefix</code></h3>
<pre><code class="language-vbnet">Function RemovePrefix(text As String, prefixLen As Long) As String
    &#x27; Remove prefix by keeping rightmost characters
    If prefixLen &gt;= Len(text) Then
        RemovePrefix = &quot;&quot;
    Else
        RemovePrefix = Right(text, Len(text) - prefixLen)
    End If
End Function</code></pre>
<h3 id="pattern-5-padlefttolength">Pattern 5: <code>PadLeftToLength</code></h3>
<pre><code class="language-vbnet">Function PadLeftToLength(text As String, totalLength As Long, _
                        Optional padChar As String = &quot; &quot;) As String
    &#x27; Pad string on the left to reach desired length
    Dim currentLen As Long
    Dim padding As String
    currentLen = Len(text)
    If currentLen &gt;= totalLength Then
        PadLeftToLength = Right(text, totalLength)
    Else
        padding = String(totalLength - currentLen, padChar)
        PadLeftToLength = padding &amp; text
    End If
End Function</code></pre>
<h3 id="pattern-6-getfilenamefrompath">Pattern 6: <code>GetFilenameFromPath</code></h3>
<pre><code class="language-vbnet">Function GetFilenameFromPath(fullPath As String) As String
    &#x27; Extract filename from full path
    Dim slashPos As Integer
    slashPos = InStrRev(fullPath, &quot;\&quot;)
    If slashPos &gt; 0 Then
        GetFilenameFromPath = Right(fullPath, Len(fullPath) - slashPos)
    Else
        GetFilenameFromPath = fullPath
    End If
End Function</code></pre>
<h3 id="pattern-7-truncateleft">Pattern 7: <code>TruncateLeft</code></h3>
<pre><code class="language-vbnet">Function TruncateLeft(text As String, maxLength As Long, _
                     Optional ellipsis As String = &quot;...&quot;) As String
    &#x27; Truncate from left, keeping rightmost characters
    Dim textLen As Long
    textLen = Len(text)
    If textLen &lt;= maxLength Then
        TruncateLeft = text
    Else
        TruncateLeft = ellipsis &amp; Right(text, maxLength - Len(ellipsis))
    End If
End Function</code></pre>
<h3 id="pattern-8-formataccountnumber">Pattern 8: <code>FormatAccountNumber</code></h3>
<pre><code class="language-vbnet">Function FormatAccountNumber(accountNum As String) As String
    &#x27; Format account number showing only last 4 digits
    Dim lastFour As String
    If Len(accountNum) &gt; 4 Then
        lastFour = Right(accountNum, 4)
        FormatAccountNumber = &quot;****&quot; &amp; lastFour
    Else
        FormatAccountNumber = accountNum
    End If
End Function</code></pre>
<h3 id="pattern-9-extractdomainextension">Pattern 9: <code>ExtractDomainExtension</code></h3>
<pre><code class="language-vbnet">Function ExtractDomainExtension(url As String) As String
    &#x27; Extract domain extension (.com, .org, etc.)
    Dim dotPos As Integer
    &#x27; Find last dot
    dotPos = InStrRev(url, &quot;.&quot;)
    If dotPos &gt; 0 Then
        ExtractDomainExtension = Right(url, Len(url) - dotPos + 1)
    Else
        ExtractDomainExtension = &quot;&quot;
    End If
End Function</code></pre>
<h3 id="pattern-10-gettrailingdigits">Pattern 10: <code>GetTrailingDigits</code></h3>
<pre><code class="language-vbnet">Function GetTrailingDigits(text As String) As String
    &#x27; Extract trailing numeric characters
    Dim i As Integer
    Dim digitCount As Integer
    digitCount = 0
    For i = Len(text) To 1 Step -1
        If IsNumeric(Mid(text, i, 1)) Then
            digitCount = digitCount + 1
        Else
            Exit For
        End If
    Next i
    If digitCount &gt; 0 Then
        GetTrailingDigits = Right(text, digitCount)
    Else
        GetTrailingDigits = &quot;&quot;
    End If
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="example-1-file-extension-validator">Example 1: File Extension Validator</h3>
<pre><code class="language-vbnet">&#x27; Comprehensive file extension validation and management
Class FileExtensionValidator
    Private m_validExtensions As Collection
    Public Sub Initialize()
        Set m_validExtensions = New Collection
    End Sub
    Public Sub AddValidExtension(extension As String)
        Dim ext As String
        &#x27; Normalize extension (add dot if missing)
        If Left(extension, 1) &lt;&gt; &quot;.&quot; Then
            ext = &quot;.&quot; &amp; extension
        Else
            ext = extension
        End If
        On Error Resume Next
        m_validExtensions.Add ext, UCase(ext)
        On Error GoTo 0
    End Sub
    Public Function IsValidFile(filename As String) As Boolean
        Dim ext As String
        Dim dotPos As Integer
        dotPos = InStrRev(filename, &quot;.&quot;)
        If dotPos = 0 Then
            IsValidFile = False
            Exit Function
        End If
        ext = Right(filename, Len(filename) - dotPos + 1)
        On Error Resume Next
        Dim temp As String
        temp = m_validExtensions(UCase(ext))
        IsValidFile = (Err.Number = 0)
        On Error GoTo 0
    End Function
    Public Function GetExtension(filename As String) As String
        Dim dotPos As Integer
        dotPos = InStrRev(filename, &quot;.&quot;)
        If dotPos &gt; 0 Then
            GetExtension = Right(filename, Len(filename) - dotPos + 1)
        Else
            GetExtension = &quot;&quot;
        End If
    End Function
    Public Function ChangeExtension(filename As String, newExt As String) As String
        Dim dotPos As Integer
        Dim baseName As String
        Dim ext As String
        dotPos = InStrRev(filename, &quot;.&quot;)
        If dotPos &gt; 0 Then
            baseName = Left(filename, dotPos - 1)
        Else
            baseName = filename
        End If
        &#x27; Normalize new extension
        If Left(newExt, 1) &lt;&gt; &quot;.&quot; Then
            ext = &quot;.&quot; &amp; newExt
        Else
            ext = newExt
        End If
        ChangeExtension = baseName &amp; ext
    End Function
    Public Function GetValidExtensions() As String
        Dim ext As Variant
        Dim result As String
        result = &quot;&quot;
        For Each ext In m_validExtensions
            If result &lt;&gt; &quot;&quot; Then result = result &amp; &quot;, &quot;
            result = result &amp; ext
        Next ext
        GetValidExtensions = result
    End Function
End Class</code></pre>
<h3 id="example-2-path-parser-module">Example 2: Path Parser Module</h3>
<pre><code class="language-vbnet">&#x27; Parse and manipulate file paths
Module PathParser
    Public Function GetFilename(fullPath As String) As String
        &#x27; Extract filename from full path
        Dim slashPos As Integer
        slashPos = InStrRev(fullPath, &quot;\&quot;)
        If slashPos &gt; 0 Then
            GetFilename = Right(fullPath, Len(fullPath) - slashPos)
        Else
            GetFilename = fullPath
        End If
    End Function
    Public Function GetDirectory(fullPath As String) As String
        &#x27; Extract directory from full path
        Dim slashPos As Integer
        slashPos = InStrRev(fullPath, &quot;\&quot;)
        If slashPos &gt; 0 Then
            GetDirectory = Left(fullPath, slashPos - 1)
        Else
            GetDirectory = &quot;&quot;
        End If
    End Function
    Public Function GetFilenameWithoutExtension(fullPath As String) As String
        &#x27; Get filename without extension
        Dim filename As String
        Dim dotPos As Integer
        filename = GetFilename(fullPath)
        dotPos = InStrRev(filename, &quot;.&quot;)
        If dotPos &gt; 0 Then
            GetFilenameWithoutExtension = Left(filename, dotPos - 1)
        Else
            GetFilenameWithoutExtension = filename
        End If
    End Function
    Public Function GetExtension(fullPath As String) As String
        &#x27; Get file extension including dot
        Dim filename As String
        Dim dotPos As Integer
        filename = GetFilename(fullPath)
        dotPos = InStrRev(filename, &quot;.&quot;)
        If dotPos &gt; 0 Then
            GetExtension = Right(filename, Len(filename) - dotPos + 1)
        Else
            GetExtension = &quot;&quot;
        End If
    End Function
    Public Function CombinePath(directory As String, filename As String) As String
        &#x27; Combine directory and filename
        If Right(directory, 1) = &quot;\&quot; Then
            CombinePath = directory &amp; filename
        Else
            CombinePath = directory &amp; &quot;\&quot; &amp; filename
        End If
    End Function
    Public Function GetParentDirectory(fullPath As String) As String
        &#x27; Get parent directory
        Dim dir As String
        Dim slashPos As Integer
        dir = GetDirectory(fullPath)
        slashPos = InStrRev(dir, &quot;\&quot;)
        If slashPos &gt; 0 Then
            GetParentDirectory = Left(dir, slashPos - 1)
        Else
            GetParentDirectory = &quot;&quot;
        End If
    End Function
End Module</code></pre>
<h3 id="example-3-string-suffix-matcher">Example 3: String Suffix Matcher</h3>
<pre><code class="language-vbnet">&#x27; Match and validate string suffixes
Class StringSuffixMatcher
    Private m_caseSensitive As Boolean
    Public Sub Initialize(Optional caseSensitive As Boolean = True)
        m_caseSensitive = caseSensitive
    End Sub
    Public Function EndsWith(text As String, suffix As String) As Boolean
        &#x27; Check if string ends with suffix
        Dim textEnd As String
        Dim suffixLen As Long
        suffixLen = Len(suffix)
        If suffixLen &gt; Len(text) Then
            EndsWith = False
            Exit Function
        End If
        textEnd = Right(text, suffixLen)
        If m_caseSensitive Then
            EndsWith = (textEnd = suffix)
        Else
            EndsWith = (UCase(textEnd) = UCase(suffix))
        End If
    End Function
    Public Function EndsWithAny(text As String, suffixes() As String) As Boolean
        &#x27; Check if string ends with any of the provided suffixes
        Dim i As Integer
        For i = LBound(suffixes) To UBound(suffixes)
            If EndsWith(text, suffixes(i)) Then
                EndsWithAny = True
                Exit Function
            End If
        Next i
        EndsWithAny = False
    End Function
    Public Function RemoveSuffix(text As String, suffix As String) As String
        &#x27; Remove suffix if present
        If EndsWith(text, suffix) Then
            RemoveSuffix = Left(text, Len(text) - Len(suffix))
        Else
            RemoveSuffix = text
        End If
    End Function
    Public Function GetMatchingSuffix(text As String, suffixes() As String) As String
        &#x27; Return the matching suffix, or empty string
        Dim i As Integer
        For i = LBound(suffixes) To UBound(suffixes)
            If EndsWith(text, suffixes(i)) Then
                GetMatchingSuffix = suffixes(i)
                Exit Function
            End If
        Next i
        GetMatchingSuffix = &quot;&quot;
    End Function
    Public Function ReplaceSuffix(text As String, oldSuffix As String, _
                                 newSuffix As String) As String
        &#x27; Replace suffix if present
        If EndsWith(text, oldSuffix) Then
            ReplaceSuffix = Left(text, Len(text) - Len(oldSuffix)) &amp; newSuffix
        Else
            ReplaceSuffix = text
        End If
    End Function
End Class</code></pre>
<h3 id="example-4-account-number-formatter">Example 4: Account Number Formatter</h3>
<pre><code class="language-vbnet">&#x27; Format and mask account numbers securely
Class AccountNumberFormatter
    Private m_maskChar As String
    Private m_visibleDigits As Integer
    Public Sub Initialize(Optional maskChar As String = &quot;*&quot;, _
                         Optional visibleDigits As Integer = 4)
        m_maskChar = maskChar
        m_visibleDigits = visibleDigits
    End Sub
    Public Function FormatAccountNumber(accountNum As String) As String
        &#x27; Format account number showing only last N digits
        Dim lastDigits As String
        Dim maskedLength As Integer
        If Len(accountNum) &lt;= m_visibleDigits Then
            FormatAccountNumber = accountNum
            Exit Function
        End If
        lastDigits = Right(accountNum, m_visibleDigits)
        maskedLength = Len(accountNum) - m_visibleDigits
        FormatAccountNumber = String(maskedLength, m_maskChar) &amp; lastDigits
    End Function
    Public Function FormatWithSpaces(accountNum As String, _
                                    Optional groupSize As Integer = 4) As String
        &#x27; Format with spaces every N characters
        Dim formatted As String
        Dim i As Integer
        Dim maskedNum As String
        maskedNum = FormatAccountNumber(accountNum)
        formatted = &quot;&quot;
        For i = 1 To Len(maskedNum) Step groupSize
            If i &gt; 1 Then formatted = formatted &amp; &quot; &quot;
            formatted = formatted &amp; Mid(maskedNum, i, groupSize)
        Next i
        FormatWithSpaces = formatted
    End Function
    Public Function GetLastDigits(accountNum As String) As String
        &#x27; Get only the visible digits
        If Len(accountNum) &lt;= m_visibleDigits Then
            GetLastDigits = accountNum
        Else
            GetLastDigits = Right(accountNum, m_visibleDigits)
        End If
    End Function
    Public Function ValidateAccountNumber(accountNum As String, _
                                         minLength As Integer, _
                                         maxLength As Integer) As Boolean
        &#x27; Validate account number length
        Dim numLen As Integer
        numLen = Len(accountNum)
        ValidateAccountNumber = (numLen &gt;= minLength And numLen &lt;= maxLength)
    End Function
    Public Sub SetMaskChar(maskChar As String)
        m_maskChar = maskChar
    End Sub
    Public Sub SetVisibleDigits(visibleDigits As Integer)
        m_visibleDigits = visibleDigits
    End Sub
End Class</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p>The <code>Right</code> function generates runtime errors in specific situations:
<strong>Error 5: Invalid procedure call or argument</strong>
- Occurs when <code>length</code> parameter is negative
<strong>Error 94: Invalid use of Null</strong>
- Occurs when <code>string</code> parameter is Null
Example error handling:</p>
<pre><code class="language-vbnet">On Error Resume Next
result = Right(userInput, charCount)
If Err.Number &lt;&gt; 0 Then
    MsgBox &quot;Error extracting characters: &quot; &amp; Err.Description
    result = &quot;&quot;
End If
On Error GoTo 0</code></pre>
<p>Best practice for safe usage:</p>
<pre><code class="language-vbnet">Function SafeRight(text As String, length As Long) As String
    If IsNull(text) Then
        SafeRight = &quot;&quot;
        Exit Function
    End If
    If length &lt; 0 Then
        SafeRight = &quot;&quot;
        Exit Function
    End If
    If length = 0 Then
        SafeRight = &quot;&quot;
    ElseIf length &gt;= Len(text) Then
        SafeRight = text
    Else
        SafeRight = Right(text, length)
    End If
End Function</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Right</code> is a very fast string function</li>
<li>Performance is O(n) where n is the length parameter</li>
<li>More efficient than using Mid to extract from the end</li>
<li>For large strings, consider caching Len(string) if called multiple times</li>
<li>No significant performance difference between Right and string slicing</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Validate Length</strong>: Check that length parameter is valid before calling</li>
<li><strong>Handle Null</strong>: Check for Null strings if data source is uncertain</li>
<li><strong>Use with <code>InStrRev</code></strong>: Combine with <code>InStrRev</code> for finding from right</li>
<li><strong>Document Intent</strong>: Make clear why extracting from right vs left</li>
<li><strong>Consider Edge Cases</strong>: Handle empty strings, length = 0, length &gt; string length</li>
<li><strong>Use for File Extensions</strong>: Preferred method for extracting file extensions</li>
<li><strong>Combine with Trim</strong>: Often useful to Trim before using Right</li>
<li><strong>Cache String Length</strong>: If using Len(string) multiple times, cache it</li>
<li><strong>Avoid Magic Numbers</strong>: Use named constants for length values</li>
<li><strong>Test Boundary Conditions</strong>: Test with length = 0, 1, Len(string), Len(string)+1</li>
</ol>
<h2 id="comparison-with-related-functions">Comparison with Related Functions</h2>
<table>
<thead>
<tr>
<th>Function</th>
<th>Purpose</th>
<th>Parameters</th>
<th>Use Case</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Right</strong></td>
<td>Extract from right</td>
<td><code>(string, length)</code></td>
<td>Get last N characters, file extensions</td>
</tr>
<tr>
<td><strong>Left</strong></td>
<td>Extract from left</td>
<td><code>(string, length)</code></td>
<td>Get first N characters, prefixes</td>
</tr>
<tr>
<td><strong>Mid</strong></td>
<td>Extract from middle</td>
<td><code>(string, start, [length])</code></td>
<td>Get substring from any position</td>
</tr>
<tr>
<td><strong><code>InStrRev</code></strong></td>
<td>Find from right</td>
<td><code>(string, substring)</code></td>
<td>Find position searching from right</td>
</tr>
<tr>
<td><strong>Len</strong></td>
<td>String length</td>
<td><code>(string)</code></td>
<td>Get total length</td>
</tr>
<tr>
<td><strong><code>RTrim</code></strong></td>
<td>Remove right spaces</td>
<td><code>(string)</code></td>
<td>Remove trailing whitespace</td>
</tr>
</tbody>
</table>
<h2 id="platform-and-version-notes">Platform and Version Notes</h2>
<ul>
<li>Available in all versions of VB6 and VBA</li>
<li>Behavior consistent across all platforms</li>
<li>In VB.NET, replaced by String.Substring or string slicing</li>
<li><code>RightB</code> and <code>RightB</code>$ variants exist for byte data</li>
<li>Right$ returns String type, Right returns Variant</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Cannot use negative length to count from a different position</li>
<li>No built-in way to extract "all but first N" characters (use Len arithmetic)</li>
<li>Raises error on negative length instead of returning empty string</li>
<li>No case-insensitive comparison built-in</li>
<li>No Unicode-aware variant (uses ANSI/DBCS)</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Left</code>: Returns characters from the left side of a string</li>
<li><code>Mid</code>: Returns characters from any position in a string</li>
<li><code>InStrRev</code>: Finds position of substring searching from right to left</li>
<li><code>Len</code>: Returns the length of a string</li>
<li><code>RTrim</code>: Removes trailing spaces from a string</li>
<li><code>InStr</code>: Finds position of substring searching from left to right</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>&copy; 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
        </div>
    </footer>
</body>
</html>