vb6parse 1.0.1

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
<!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 - rtrim - String">
    <title>rtrim - 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> / rtrim</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="rtrim-function">RTrim Function</h1>
<p>Returns a String containing a copy of a specified string with trailing spaces removed.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">RTrim(string)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>string</code> (Required): String expression from which trailing spaces are to be removed</li>
<li>Can be any valid string expression</li>
<li>If string is Null, returns Null</li>
<li>Empty string returns empty string</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a String (or Variant):
- Copy of string with trailing spaces removed
- Removes only spaces (ASCII 32) from the right
- Does not remove tabs, newlines, or other whitespace characters
- Returns Null if input is Null
- Returns empty string if input is empty or all spaces
- Leading spaces are preserved
- Internal spaces are preserved</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>RTrim</code> function removes trailing spaces:
- Removes only space characters (ASCII 32) from the right side
- Does not remove tabs (Chr(9)), line feeds (Chr(10)), or carriage returns (Chr(13))
- Does not remove non-breaking spaces or other Unicode whitespace
- Leading spaces are not affected
- Internal spaces between words are preserved
- Often used to clean up output formatting
- Commonly paired with <code>LTrim</code> or used with Trim
- Null input returns Null (propagates Null)
- Empty string input returns empty string
- String of only spaces returns empty string
- Does not modify the original string (returns new string)
- Can be used with Variant variables
- Common in report generation and text alignment
- Used to remove padding from fixed-width fields
- Essential for cleaning exported data
- Part of the VB6 string manipulation library
- Available in all VB versions
- Related to <code>LTrim</code> (removes leading spaces) and Trim (removes both)</p>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Remove Trailing Spaces</strong></li>
</ol>
<pre><code class="language-vbnet">   cleanText = RTrim(&quot;Hello   &quot;)
   ```
2. **Clean Fixed-Width Output**
   ```vb
   outputLine = RTrim(paddedField)
   ```
3. **Format Display**
   ```vb
   lblName.Caption = RTrim(recordset(&quot;Name&quot;))
   ```
4. **Process Data Export**
   ```vb
   exportValue = RTrim(databaseField)
   ```
5. **Normalize Text**
   ```vb
   normalizedText = LTrim(RTrim(inputText))
   ```
6. **String Comparison**
   ```vb
   If RTrim(text1) = RTrim(text2) Then
       &#x27; Equal when ignoring trailing spaces
   End If
   ```
7. **Write to File**
   ```vb
   Print #1, RTrim(record.Field1)
   ```
8. **Report Generation**
   ```vb
   reportLine = RTrim(customerName) &amp; &quot; - &quot; &amp; orderID
   ```
## Basic Examples
### Example 1: Basic Usage</code></pre>
<p>vb
Dim result As String
result = RTrim("Hello   ")           ' Returns "Hello"
result = RTrim("   Hello")           ' Returns "   Hello" (leading preserved)
result = RTrim("   Hello World   ")  ' Returns "   Hello World"
result = RTrim("NoSpaces")           ' Returns "NoSpaces"
result = RTrim("     ")              ' Returns ""
result = RTrim("")                   ' Returns ""</p>
<pre><code>### Example 2: Fixed-Width File Export</code></pre>
<p>vb
Sub ExportToFixedWidth(ByVal filename As String)
    Dim rs As ADODB.Recordset
    Dim fileNum As Integer
    Dim line As String
    Set rs = New ADODB.Recordset
    rs.Open "SELECT * FROM Customers", conn
    fileNum = FreeFile
    Open filename For Output As #fileNum
    Do While Not rs.EOF
        ' Build fixed-width line and remove trailing spaces
        line = Left(rs("CustomerID") &amp; Space(10), 10) &amp; _
               Left(rs("CompanyName") &amp; Space(40), 40) &amp; _
               Left(rs("City") &amp; Space(20), 20)
        ' Write without trailing spaces
        Print #fileNum, RTrim(line)
        rs.MoveNext
    Loop
    Close #fileNum
    rs.Close
    Set rs = Nothing
End Sub</p>
<pre><code>### Example 3: Database Field Cleanup</code></pre>
<p>vb
Function GetCustomerName(ByVal customerID As String) As String
    Dim rs As ADODB.Recordset
    Dim sql As String
    sql = "SELECT CustomerName FROM Customers WHERE CustomerID = '" &amp; customerID &amp; "'"
    Set rs = New ADODB.Recordset
    rs.Open sql, conn
    If Not rs.EOF Then
        ' Remove trailing spaces from database field (may be CHAR type)
        GetCustomerName = RTrim(rs("CustomerName") &amp; "")
    Else
        GetCustomerName = ""
    End If
    rs.Close
    Set rs = Nothing
End Function</p>
<pre><code>### Example 4: Report Formatting</code></pre>
<p>vb
Sub GenerateReport()
    Dim rs As ADODB.Recordset
    Dim reportLine As String
    Set rs = New ADODB.Recordset
    rs.Open "SELECT * FROM Orders", conn
    lstReport.Clear
    Do While Not rs.EOF
        ' Format report line with proper spacing
        reportLine = RTrim(rs("CustomerName") &amp; "") &amp; " - " &amp; _
                    "Order #" &amp; rs("OrderID") &amp; " - " &amp; _
                    Format(rs("OrderDate"), "mm/dd/yyyy")
        lstReport.AddItem reportLine
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
End Sub</p>
<pre><code>## Common Patterns
### Pattern 1: `FullTrim` (combine with `LTrim`)</code></pre>
<p>vb
Function FullTrim(ByVal text As String) As String
    FullTrim = LTrim(RTrim(text))
    ' Note: Can also use built-in Trim() function
End Function</p>
<pre><code>### Pattern 2: `SafeRTrim` (handle Null)</code></pre>
<p>vb
Function SafeRTrim(ByVal text As Variant) As String
    If IsNull(text) Then
        SafeRTrim = ""
    Else
        SafeRTrim = RTrim(text)
    End If
End Function</p>
<pre><code>### Pattern 3: `CleanDatabaseField`</code></pre>
<p>vb
Function CleanDatabaseField(ByVal rs As Recordset, _
                           ByVal fieldName As String) As String
    If Not IsNull(rs(fieldName)) Then
        CleanDatabaseField = RTrim(rs(fieldName) &amp; "")
    Else
        CleanDatabaseField = ""
    End If
End Function</p>
<pre><code>### Pattern 4: `TrimTrailingSpaces`</code></pre>
<p>vb
Sub TrimTrailingSpaces(fields() As String)
    Dim i As Integer
    For i = LBound(fields) To UBound(fields)
        fields(i) = RTrim(fields(i))
    Next i
End Sub</p>
<pre><code>### Pattern 5: `CompareIgnoreTrailing`</code></pre>
<p>vb
Function CompareIgnoreTrailing(ByVal str1 As String, _
                               ByVal str2 As String) As Boolean
    CompareIgnoreTrailing = (RTrim(str1) = RTrim(str2))
End Function</p>
<pre><code>### Pattern 6: `FormatFixedWidth`</code></pre>
<p>vb
Function FormatFixedWidth(ByVal text As String, _
                         ByVal width As Integer) As String
    Dim padded As String
    padded = Left(text &amp; Space(width), width)
    FormatFixedWidth = RTrim(padded)
End Function</p>
<pre><code>### Pattern 7: `CleanRecordsetField`</code></pre>
<p>vb
Function CleanRecordsetField(ByVal rs As Recordset, _
                            ByVal fieldName As String, _
                            Optional ByVal defaultValue As String = "") As String
    On Error Resume Next
    If Not IsNull(rs(fieldName)) Then
        CleanRecordsetField = RTrim(CStr(rs(fieldName)))
    Else
        CleanRecordsetField = defaultValue
    End If
End Function</p>
<pre><code>### Pattern 8: `ExportCleanLine`</code></pre>
<p>vb
Sub ExportCleanLine(ByVal fileNum As Integer, _
                    ByVal text As String)
    Print #fileNum, RTrim(text)
End Sub</p>
<pre><code>### Pattern 9: `RemoveTrailingPadding`</code></pre>
<p>vb
Function RemoveTrailingPadding(ByVal paddedText As String) As String
    RemoveTrailingPadding = RTrim(paddedText)
End Function</p>
<pre><code>### Pattern 10: `CleanDisplayText`</code></pre>
<p>vb
Sub CleanDisplayText(ByVal ctrl As Control)
    If TypeOf ctrl Is Label Or TypeOf ctrl Is TextBox Then
        ctrl.Caption = RTrim(ctrl.Caption)
    End If
End Sub</p>
<pre><code>## Advanced Examples
### Example 1: Fixed-Width File Processor</code></pre>
<p>vb
' Class: FixedWidthExporter
Private m_fileNum As Integer
Private m_isOpen As Boolean
Public Sub OpenFile(ByVal filename As String)
    If m_isOpen Then CloseFile
    m_fileNum = FreeFile
    Open filename For Output As #m_fileNum
    m_isOpen = True
End Sub
Public Sub WriteField(ByVal text As String, ByVal width As Integer)
    Dim padded As String
    ' Pad to width and remove trailing spaces
    padded = Left(text &amp; Space(width), width)
    Print #m_fileNum, RTrim(padded);
End Sub
Public Sub WriteLine()
    Print #m_fileNum, ""
End Sub
Public Sub WriteRecord(ParamArray fields() As Variant)
    Dim i As Integer
    Dim line As String
    For i = LBound(fields) To UBound(fields)
        If i Mod 2 = 0 Then
            ' Even index = text
            line = line &amp; Left(fields(i) &amp; Space(fields(i + 1)), fields(i + 1))
        End If
    Next i
    Print #m_fileNum, RTrim(line)
End Sub
Public Sub CloseFile()
    If m_isOpen Then
        Close #m_fileNum
        m_isOpen = False
    End If
End Sub
Private Sub Class_Terminate()
    CloseFile
End Sub</p>
<pre><code>### Example 2: Database Field Cleaner</code></pre>
<p>vb
' Class: RecordsetCleaner
Private m_trimLeading As Boolean
Private m_trimTrailing As Boolean
Private Sub Class_Initialize()
    m_trimLeading = False
    m_trimTrailing = True
End Sub
Public Property Let TrimLeading(ByVal value As Boolean)
    m_trimLeading = value
End Property
Public Property Let TrimTrailing(ByVal value As Boolean)
    m_trimTrailing = value
End Property
Public Function GetCleanField(ByVal rs As Recordset, _
                              ByVal fieldName As String) As String
    Dim value As String
    If IsNull(rs(fieldName)) Then
        GetCleanField = ""
        Exit Function
    End If
    value = CStr(rs(fieldName))
    If m_trimTrailing Then
        value = RTrim(value)
    End If
    If m_trimLeading Then
        value = LTrim(value)
    End If
    GetCleanField = value
End Function
Public Sub CleanRecordset(ByVal rs As Recordset)
    Dim fld As Field
    If rs.EOF And rs.BOF Then Exit Sub
    rs.MoveFirst
    Do While Not rs.EOF
        For Each fld In rs.Fields
            If fld.Type = adVarChar Or fld.Type = adChar Then
                If Not IsNull(fld.Value) Then
                    fld.Value = GetCleanField(rs, fld.Name)
                End If
            End If
        Next fld
        rs.MoveNext
    Loop
End Sub</p>
<pre><code>### Example 3: Report Generator</code></pre>
<p>vb
' Class: ReportGenerator
Private m_lines As Collection
Private m_columnWidths() As Integer
Public Sub Initialize(columnWidths() As Integer)
    Set m_lines = New Collection
    m_columnWidths = columnWidths
End Sub
Public Sub AddRow(ParamArray values() As Variant)
    Dim i As Integer
    Dim line As String
    Dim cellValue As String
    For i = LBound(values) To UBound(values)
        If i &lt;= UBound(m_columnWidths) Then
            cellValue = CStr(values(i))
            line = line &amp; Left(cellValue &amp; Space(m_columnWidths(i)), _
                               m_columnWidths(i))
        End If
    Next i
    ' Remove trailing spaces from line
    m_lines.Add RTrim(line)
End Sub
Public Sub AddSeparator()
    Dim i As Integer
    Dim line As String
    For i = LBound(m_columnWidths) To UBound(m_columnWidths)
        line = line &amp; String(m_columnWidths(i), "-")
    Next i
    m_lines.Add RTrim(line)
End Sub
Public Function GetReport() As String
    Dim line As Variant
    Dim report As String
    For Each line In m_lines
        report = report &amp; line &amp; vbCrLf
    Next line
    GetReport = report
End Function
Public Sub SaveToFile(ByVal filename As String)
    Dim fileNum As Integer
    Dim line As Variant
    fileNum = FreeFile
    Open filename For Output As #fileNum
    For Each line In m_lines
        Print #fileNum, line
    Next line
    Close #fileNum
End Sub</p>
<pre><code>### Example 4: Text Utilities Module</code></pre>
<p>vb
' Module: TextUtils
Public Function CleanTrailing(ByVal text As String) As String
    CleanTrailing = RTrim(text)
End Function
Public Function CleanBoth(ByVal text As String) As String
    CleanBoth = LTrim(RTrim(text))
End Function
Public Function CleanArray(arr() As String, _
                          ByVal trimType As String) As String()
    Dim i As Integer
    Dim result() As String
    ReDim result(LBound(arr) To UBound(arr))
    For i = LBound(arr) To UBound(arr)
        Select Case LCase(trimType)
            Case "trailing", "right"
                result(i) = RTrim(arr(i))
            Case "leading", "left"
                result(i) = LTrim(arr(i))
            Case "both", "all"
                result(i) = LTrim(RTrim(arr(i)))
            Case Else
                result(i) = arr(i)
        End Select
    Next i
    CleanArray = result
End Function
Public Function PadAndTrim(ByVal text As String, _
                          ByVal width As Integer, _
                          Optional ByVal padChar As String = " ") As String
    Dim padded As String
    If Len(padChar) = 0 Then padChar = " "
    padded = Left(text &amp; String(width, padChar), width)
    PadAndTrim = RTrim(padded)
End Function
Public Function JoinClean(arr() As String, _
                         ByVal delimiter As String) As String
    Dim i As Integer
    Dim result As String
    Dim cleanValue As String
    For i = LBound(arr) To UBound(arr)
        cleanValue = RTrim(arr(i))
        If cleanValue &lt;&gt; "" Then
            If result &lt;&gt; "" Then
                result = result &amp; delimiter
            End If
            result = result &amp; cleanValue
        End If
    Next i
    JoinClean = result
End Function</p>
<pre><code>## Error Handling</code></pre>
<p>vb
' RTrim handles Null gracefully
Dim result As Variant
result = RTrim(Null)  ' Returns Null
' Safe trimming with Null check
Function SafeRTrim(ByVal value As Variant) As String
    If IsNull(value) Then
        SafeRTrim = ""
    Else
        SafeRTrim = RTrim(CStr(value))
    End If
End Function</p>
<pre><code>## Performance Considerations
- **Fast Operation**: String trimming is highly optimized
- **Creates New String**: Does not modify original (immutable)
- **Avoid in Tight Loops**: Cache result if using multiple times
- **Use `Trim()` for Both**: If removing both leading and trailing spaces
## Best Practices
1. **Use for database CHAR fields** - Remove padding from fixed-length fields
2. **Clean export data** - Remove trailing spaces before writing to files
3. **Combine with `LTrim`** - Use `Trim()` instead for both sides
4. **Validate before use** - Check for Null if using Variant
5. **Cache trimmed values** - Don&#x27;t call repeatedly in loops
6. **Apply to report output** - Clean formatting in generated reports
7. **Use with fixed-width formats** - Essential for proper alignment
8. **Document expectations** - Clarify if tabs/newlines should be removed
9. **Test edge cases** - Empty strings, all spaces, Null values
10. **Consider Unicode** - `RTrim` only removes ASCII space (32)
## Comparison with Related Functions
| Function | Removes Leading | Removes Trailing | Removes Both |
|----------|----------------|------------------|--------------|
| **`RTrim`** | No | Yes | No |
| **`LTrim`** | Yes | No | No |
| **Trim** | Yes | Yes | Yes |
## `RTrim` vs `LTrim` vs Trim</code></pre>
<p>vb
Dim text As String
text = "   Hello World   "
' RTrim - removes trailing spaces only
Debug.Print "[" &amp; RTrim(text) &amp; "]"   ' [   Hello World]
' LTrim - removes leading spaces only
Debug.Print "[" &amp; LTrim(text) &amp; "]"   ' [Hello World   ]
' Trim - removes both leading and trailing
Debug.Print "[" &amp; Trim(text) &amp; "]"    ' [Hello World]
' Manual equivalent to Trim
Debug.Print "[" &amp; LTrim(RTrim(text)) &amp; "]"  ' [Hello World]</p>
<pre><code>## Whitespace Characters</code></pre>
<p>vb
' RTrim only removes space (ASCII 32)
Dim text As String
text = "Hello   "        ' Spaces - REMOVED
text = "Hello" &amp; Chr(9)  ' Tab - NOT REMOVED
text = "Hello" &amp; Chr(10) ' Line feed - NOT REMOVED
text = "Hello" &amp; Chr(13) ' Carriage return - NOT REMOVED
text = "Hello" &amp; Chr(160) ' Non-breaking space - NOT REMOVED
' To remove other whitespace, use custom function
Function TrimAllWhitespaceRight(ByVal text As String) As String
    Do While Len(text) &gt; 0
        Dim ch As String
        ch = Right(text, 1)
        If ch = " " Or ch = Chr(9) Or ch = Chr(10) Or ch = Chr(13) Then
            text = Left(text, Len(text) - 1)
        Else
            Exit Do
        End If
    Loop
    TrimAllWhitespaceRight = text
End Function
```</p>
<h2 id="platform-notes">Platform Notes</h2>
<ul>
<li>Available in all VB6 versions</li>
<li>Part of VBA core library</li>
<li>Works with ANSI and Unicode strings</li>
<li>Only removes ASCII space character (32)</li>
<li>Returns new string (original unchanged)</li>
<li>Handles Null by returning Null</li>
<li>Available in <code>VBScript</code></li>
<li>Same behavior across all Windows versions</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li><strong>Only Space Character</strong>: Does not remove tabs, line feeds, etc.</li>
<li><strong>No Unicode Whitespace</strong>: Does not remove non-breaking spaces, em spaces, etc.</li>
<li><strong>Creates New String</strong>: Cannot modify string in place</li>
<li><strong>No Custom Characters</strong>: Cannot specify which characters to remove</li>
<li><strong>Null Propagation</strong>: Returns Null if input is Null</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>LTrim</code>: Removes leading spaces from string</li>
<li><code>Trim</code>: Removes both leading and trailing spaces</li>
<li><code>Right</code>: Returns rightmost characters</li>
<li><code>Left</code>: Returns leftmost characters</li>
<li><code>Mid</code>: Returns substring from middle</li>
<li><code>Replace</code>: Replaces occurrences of substring</li>
<li><code>Space</code>: Creates string of spaces</li>
<li><code>Len</code>: Returns string length</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>