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
<!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 - len - String">
    <title>len - 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> / len</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="len-function">Len Function</h1>
<p>Returns a <code>Long</code> containing the number of characters in a string or the number of bytes required to store a variable.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Len(string | varname)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>string</code> (Optional): Any valid string expression</li>
<li>If string contains <code>Null</code>, <code>Null</code> is returned</li>
<li><code>varname</code> (Optional): Any valid variable name</li>
<li>For user-defined types, returns size in bytes</li>
<li>For objects, may return implementation-defined value</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a Long:
- For strings: Number of characters in the string
- For variables: Number of bytes required to store the variable
- Returns 0 for empty string ("")
- Returns <code>Null</code> if string argument is <code>Null</code>
- For fixed-length strings: Returns declared length
- For Variant containing string: Returns length of string
- For user-defined types: Returns total size in bytes</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Len</code> function measures string length or variable size:
- Most commonly used for string length
- Returns character count, not byte count (for strings)
- Empty string ("") returns 0
- Null propagates through the function
- Spaces and special characters are counted
- For fixed-length strings (e.g., String * 10), returns declared length
- Variable-length strings return actual content length
- <code>LenB</code> function returns byte count (useful for Unicode/DBCS)
- Cannot determine array size (use UBound/LBound instead)
- Essential for string manipulation and validation
- Used with Left, Right, Mid for substring operations
- Common in loops iterating through string characters
- Efficient operation with minimal overhead
- For user-defined types, returns structure size
- Object variables may return unpredictable values</p>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Validate Input</strong>: Check if string is empty or within length limits</li>
<li><strong>String Iteration</strong>: Loop through each character in string</li>
<li><strong>Substring Extraction</strong>: Calculate positions for Left/Right/Mid</li>
<li><strong>Text Processing</strong>: Measure text before truncation or padding</li>
<li><strong>File I/O</strong>: Determine buffer sizes for string data</li>
<li><strong>Data Validation</strong>: Ensure fields meet length requirements</li>
<li><strong>Format Verification</strong>: Check string format compliance</li>
<li><strong>Memory Calculation</strong>: Determine structure sizes</li>
</ol>
<h2 id="basic-usage-examples">Basic Usage Examples</h2>
<pre><code class="language-vbnet">&#x27; Example 1: Basic string length
Dim text As String
text = &quot;Hello World&quot;
Debug.Print Len(text)                &#x27; 11 - includes space
Debug.Print Len(&quot;VB6&quot;)               &#x27; 3
Debug.Print Len(&quot;&quot;)                  &#x27; 0 - empty string
&#x27; Example 2: Validation
Dim password As String
password = &quot;secret123&quot;
If Len(password) &lt; 8 Then
    MsgBox &quot;Password must be at least 8 characters&quot;
End If
&#x27; Example 3: String iteration
Dim i As Long
Dim char As String
text = &quot;ABC&quot;
For i = 1 To Len(text)
    char = Mid(text, i, 1)
    Debug.Print char
Next i
&#x27; Example 4: Null handling
Dim value As Variant
value = Null
Debug.Print IsNull(Len(value))       &#x27; True - Null propagates</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<pre><code class="language-vbnet">&#x27; Pattern 1: Check if string is empty
Function IsEmpty(text As String) As Boolean
    IsEmpty = (Len(text) = 0)
End Function
&#x27; Pattern 2: Validate string length range
Function ValidateLength(text As String, minLen As Long, maxLen As Long) As Boolean
    Dim length As Long
    length = Len(text)
    ValidateLength = (length &gt;= minLen And length &lt;= maxLen)
End Function
&#x27; Pattern 3: Pad string to fixed width
Function PadRight(text As String, width As Long, Optional padChar As String = &quot; &quot;) As String
    If Len(text) &gt;= width Then
        PadRight = Left(text, width)
    Else
        PadRight = text &amp; String(width - Len(text), padChar)
    End If
End Function
&#x27; Pattern 4: Center text in field
Function CenterText(text As String, width As Long) As String
    Dim padding As Long
    If Len(text) &gt;= width Then
        CenterText = Left(text, width)
    Else
        padding = (width - Len(text)) \ 2
        CenterText = String(padding, &quot; &quot;) &amp; text &amp; _
                     String(width - Len(text) - padding, &quot; &quot;)
    End If
End Function
&#x27; Pattern 5: Reverse string
Function ReverseString(text As String) As String
    Dim i As Long
    Dim result As String
    result = &quot;&quot;
    For i = Len(text) To 1 Step -1
        result = result &amp; Mid(text, i, 1)
    Next i
    ReverseString = result
End Function
&#x27; Pattern 6: Count occurrences of character
Function CountChar(text As String, searchChar As String) As Long
    Dim i As Long
    Dim count As Long
    count = 0
    For i = 1 To Len(text)
        If Mid(text, i, 1) = searchChar Then
            count = count + 1
        End If
    Next i
    CountChar = count
End Function
&#x27; Pattern 7: Truncate with ellipsis
Function TruncateText(text As String, maxLength As Long) As String
    If Len(text) &lt;= maxLength Then
        TruncateText = text
    ElseIf maxLength &lt;= 3 Then
        TruncateText = Left(text, maxLength)
    Else
        TruncateText = Left(text, maxLength - 3) &amp; &quot;...&quot;
    End If
End Function
&#x27; Pattern 8: Extract last N characters
Function GetLastChars(text As String, count As Long) As String
    If count &gt;= Len(text) Then
        GetLastChars = text
    Else
        GetLastChars = Right(text, count)
    End If
End Function
&#x27; Pattern 9: Check minimum length
Function HasMinLength(text As String, minLength As Long) As Boolean
    HasMinLength = (Len(text) &gt;= minLength)
End Function
&#x27; Pattern 10: Calculate character position from end
Function GetCharFromEnd(text As String, posFromEnd As Long) As String
    Dim pos As Long
    pos = Len(text) - posFromEnd + 1
    If pos &gt;= 1 And pos &lt;= Len(text) Then
        GetCharFromEnd = Mid(text, pos, 1)
    Else
        GetCharFromEnd = &quot;&quot;
    End If
End Function</code></pre>
<h2 id="advanced-usage-examples">Advanced Usage Examples</h2>
<pre><code class="language-vbnet">&#x27; Example 1: String analyzer
Public Class StringAnalyzer
    Public Function GetStats(text As String) As String
        Dim length As Long
        Dim words As Long
        Dim lines As Long
        Dim result As String
        length = Len(text)
        words = UBound(Split(text, &quot; &quot;)) + 1
        lines = UBound(Split(text, vbCrLf)) + 1
        result = &quot;Length: &quot; &amp; length &amp; vbCrLf
        result = result &amp; &quot;Words: &quot; &amp; words &amp; vbCrLf
        result = result &amp; &quot;Lines: &quot; &amp; lines
        GetStats = result
    End Function
    Public Function GetCharFrequency(text As String) As Object
        Dim dict As Object
        Dim i As Long
        Dim char As String
        Set dict = CreateObject(&quot;Scripting.Dictionary&quot;)
        For i = 1 To Len(text)
            char = Mid(text, i, 1)
            If dict.Exists(char) Then
                dict(char) = dict(char) + 1
            Else
                dict.Add char, 1
            End If
        Next i
        Set GetCharFrequency = dict
    End Function
    Public Function IsPalindrome(text As String) As Boolean
        Dim i As Long
        Dim length As Long
        length = Len(text)
        For i = 1 To length \ 2
            If Mid(text, i, 1) &lt;&gt; Mid(text, length - i + 1, 1) Then
                IsPalindrome = False
                Exit Function
            End If
        Next i
        IsPalindrome = True
    End Function
End Class
&#x27; Example 2: Text formatter
Public Class TextFormatter
    Public Function WordWrap(text As String, lineWidth As Long) As String
        Dim result As String
        Dim currentLine As String
        Dim words() As String
        Dim i As Long
        Dim word As String
        result = &quot;&quot;
        currentLine = &quot;&quot;
        words = Split(text, &quot; &quot;)
        For i = LBound(words) To UBound(words)
            word = words(i)
            If Len(currentLine) = 0 Then
                currentLine = word
            ElseIf Len(currentLine) + 1 + Len(word) &lt;= lineWidth Then
                currentLine = currentLine &amp; &quot; &quot; &amp; word
            Else
                result = result &amp; currentLine &amp; vbCrLf
                currentLine = word
            End If
        Next i
        If Len(currentLine) &gt; 0 Then
            result = result &amp; currentLine
        End If
        WordWrap = result
    End Function
    Public Function JustifyText(text As String, width As Long) As String
        Dim words() As String
        Dim totalLen As Long
        Dim gaps As Long
        Dim extraSpaces As Long
        Dim spacesPerGap As Long
        Dim result As String
        Dim i As Long
        words = Split(text, &quot; &quot;)
        If UBound(words) = 0 Then
            JustifyText = text
            Exit Function
        End If
        &#x27; Calculate total word length
        totalLen = 0
        For i = LBound(words) To UBound(words)
            totalLen = totalLen + Len(words(i))
        Next i
        gaps = UBound(words) - LBound(words)
        extraSpaces = width - totalLen
        spacesPerGap = extraSpaces \ gaps
        result = &quot;&quot;
        For i = LBound(words) To UBound(words)
            result = result &amp; words(i)
            If i &lt; UBound(words) Then
                result = result &amp; String(spacesPerGap, &quot; &quot;)
            End If
        Next i
        JustifyText = result
    End Function
End Class
&#x27; Example 3: Input validator
Public Class InputValidator
    Public Function ValidateEmail(email As String) As Boolean
        &#x27; Basic email validation
        If Len(email) = 0 Then
            ValidateEmail = False
            Exit Function
        End If
        If InStr(email, &quot;@&quot;) = 0 Then
            ValidateEmail = False
            Exit Function
        End If
        If InStr(email, &quot;.&quot;) = 0 Then
            ValidateEmail = False
            Exit Function
        End If
        ValidateEmail = True
    End Function
    Public Function ValidatePassword(password As String) As String
        Dim errors As Collection
        Dim i As Long
        Dim char As String
        Dim hasUpper As Boolean
        Dim hasLower As Boolean
        Dim hasDigit As Boolean
        Set errors = New Collection
        If Len(password) &lt; 8 Then
            errors.Add &quot;Password must be at least 8 characters&quot;
        End If
        If Len(password) &gt; 128 Then
            errors.Add &quot;Password must not exceed 128 characters&quot;
        End If
        hasUpper = False
        hasLower = False
        hasDigit = False
        For i = 1 To Len(password)
            char = Mid(password, i, 1)
            If char &gt;= &quot;A&quot; And char &lt;= &quot;Z&quot; Then hasUpper = True
            If char &gt;= &quot;a&quot; And char &lt;= &quot;z&quot; Then hasLower = True
            If char &gt;= &quot;0&quot; And char &lt;= &quot;9&quot; Then hasDigit = True
        Next i
        If Not hasUpper Then errors.Add &quot;Password must contain uppercase letter&quot;
        If Not hasLower Then errors.Add &quot;Password must contain lowercase letter&quot;
        If Not hasDigit Then errors.Add &quot;Password must contain digit&quot;
        If errors.Count = 0 Then
            ValidatePassword = &quot;&quot;
        Else
            Dim msg As String
            msg = &quot;&quot;
            For i = 1 To errors.Count
                msg = msg &amp; errors(i) &amp; vbCrLf
            Next i
            ValidatePassword = msg
        End If
    End Function
    Public Function ValidateLength(text As String, fieldName As String, _
                                   minLen As Long, maxLen As Long) As String
        Dim length As Long
        length = Len(text)
        If length &lt; minLen Then
            ValidateLength = fieldName &amp; &quot; must be at least &quot; &amp; minLen &amp; &quot; characters&quot;
        ElseIf length &gt; maxLen Then
            ValidateLength = fieldName &amp; &quot; must not exceed &quot; &amp; maxLen &amp; &quot; characters&quot;
        Else
            ValidateLength = &quot;&quot;
        End If
    End Function
End Class
&#x27; Example 4: String builder with size tracking
Public Class StringBuilder
    Private m_buffer As String
    Private m_length As Long
    Private Sub Class_Initialize()
        m_buffer = &quot;&quot;
        m_length = 0
    End Sub
    Public Sub Append(text As String)
        m_buffer = m_buffer &amp; text
        m_length = m_length + Len(text)
    End Sub
    Public Sub AppendLine(Optional text As String = &quot;&quot;)
        Append text &amp; vbCrLf
    End Sub
    Public Function ToString() As String
        ToString = m_buffer
    End Function
    Public Property Get Length() As Long
        Length = m_length
    End Property
    Public Sub Clear()
        m_buffer = &quot;&quot;
        m_length = 0
    End Sub
    Public Function Substring(startIndex As Long, length As Long) As String
        If startIndex &lt; 0 Or startIndex &gt;= m_length Then
            Substring = &quot;&quot;
            Exit Function
        End If
        Substring = Mid(m_buffer, startIndex + 1, length)
    End Function
End Class</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p>Len handles special cases gracefully:</p>
<pre><code class="language-vbnet">&#x27; Empty string returns 0
Debug.Print Len(&quot;&quot;)                  &#x27; 0
&#x27; Null propagates
Dim value As Variant
value = Null
Debug.Print IsNull(Len(value))       &#x27; True
&#x27; Spaces are counted
Debug.Print Len(&quot;   &quot;)               &#x27; 3
&#x27; Special characters are counted
Debug.Print Len(vbCrLf)              &#x27; 2 - CR + LF
Debug.Print Len(vbTab)               &#x27; 1
&#x27; Safe pattern with Null check
Function SafeLen(value As Variant) As Long
    If IsNull(value) Then
        SafeLen = 0
    Else
        SafeLen = Len(value)
    End If
End Function</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><strong>Fast Operation</strong>: <code>Len</code> is very fast, minimal overhead</li>
<li><strong>Cache Result</strong>: If checking length multiple times, cache the value</li>
<li><strong>String Iteration</strong>: For large strings, cache <code>Len</code> result in loop variable</li>
<li><strong>No Side Effects</strong>: <code>Len</code> does not modify the string
Performance tips:</li>
</ul>
<pre><code class="language-vbnet">&#x27; Less efficient - calls Len every iteration
For i = 1 To Len(text)
    &#x27; process
Next i
&#x27; More efficient - cache length
Dim length As Long
length = Len(text)
For i = 1 To length
    &#x27; process
Next i</code></pre>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Validate Input</strong>: Always check string length for validation</li>
<li><strong><code>Null</code> Safety</strong>: Handle <code>Null</code> values before calling <code>Len</code> if needed</li>
<li><strong>Cache Results</strong>: Store length in variable when used multiple times</li>
<li><strong>Empty Check</strong>: Use <code>Len(str) = 0</code> to check for empty strings</li>
<li><strong>Loop Optimization</strong>: Cache <code>Len</code> result before loops</li>
<li><strong>Bounds Checking</strong>: Verify length before substring operations</li>
<li><strong>Fixed-Length Strings</strong>: Remember <code>Len</code> returns declared length, not content</li>
<li><strong>Database Fields</strong>: Validate field lengths before database inserts</li>
</ol>
<h2 id="comparison-with-related-functions">Comparison with Related Functions</h2>
<table>
<thead>
<tr>
<th>Function</th>
<th>Purpose</th>
<th>Returns</th>
<th>Use Case</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Len</code></td>
<td>Get string length</td>
<td><code>Long</code></td>
<td>Character count</td>
</tr>
<tr>
<td><code>LenB</code></td>
<td>Get byte length</td>
<td><code>Long</code></td>
<td>Byte count (Unicode/DBCS)</td>
</tr>
<tr>
<td><code>UBound</code></td>
<td>Get array upper bound</td>
<td><code>Long</code></td>
<td>Array size calculation</td>
</tr>
<tr>
<td><code>InStr</code></td>
<td>Find substring</td>
<td><code>Long</code></td>
<td>Substring position</td>
</tr>
<tr>
<td><code>Left</code>/<code>Right</code>/<code>Mid</code></td>
<td>Extract substring</td>
<td><code>String</code></td>
<td>Substring extraction</td>
</tr>
</tbody>
</table>
<h2 id="len-vs-lenb"><code>Len</code> vs <code>LenB</code></h2>
<pre><code class="language-vbnet">Dim text As String
text = &quot;Hello&quot;
&#x27; Len - character count
Debug.Print Len(text)                &#x27; 5 characters
&#x27; LenB - byte count (may differ for Unicode)
Debug.Print LenB(text)               &#x27; 10 bytes (2 bytes per char in Unicode)
&#x27; For ASCII characters, LenB is typically 2 * Len
&#x27; For DBCS characters, varies by character</code></pre>
<h2 id="fixed-length-vs-variable-length-strings">Fixed-Length vs Variable-Length Strings</h2>
<pre><code class="language-vbnet">&#x27; Variable-length string - Len returns actual content length
Dim varStr As String
varStr = &quot;ABC&quot;
Debug.Print Len(varStr)              &#x27; 3
&#x27; Fixed-length string - Len returns declared length
Dim fixStr As String * 10
fixStr = &quot;ABC&quot;
Debug.Print Len(fixStr)              &#x27; 10 (includes padding spaces)</code></pre>
<h2 id="platform-and-version-notes">Platform and Version Notes</h2>
<ul>
<li>Available in all VB6 versions</li>
<li>Part of VBA core functions</li>
<li>Returns Long type</li>
<li>Maximum string length in VB6 is approximately 2GB (theoretical)</li>
<li>Practical limit much lower due to memory constraints</li>
<li>For user-defined types, returns total structure size in bytes</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Returns character count, not byte count (use <code>LenB</code> for bytes)</li>
<li>Cannot measure array size directly (use <code>UBound - LBound + 1</code>)</li>
<li>For objects, may return unpredictable values</li>
<li>For fixed-length strings, returns declared length, not trimmed length</li>
<li>Does not distinguish between different whitespace characters</li>
<li>No built-in way to get "visible" length (excluding control characters)</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>LenB</code>: Get byte length of string or variable</li>
<li><code>Left</code>/<code>Right</code>/<code>Mid</code>: Extract substrings (often used with Len)</li>
<li><code>InStr</code>/<code>InStrRev</code>: Find substring positions</li>
<li><code>Trim</code>/<code>LTrim</code>/<code>RTrim</code>: Remove whitespace</li>
<li><code>String</code>: Create string of repeated characters</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>