<!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 - space_dollar - String">
<title>space_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> / space_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="space-function">Space$ Function</h1>
<p>The <code>Space$</code> function in Visual Basic 6 returns a string consisting of the specified number
of space characters (ASCII 32). 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">Space$(number)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>number</code> - Required. Long integer indicating the number of spaces to include in the string.
Must be between 0 and approximately 2 billion (2,147,483,647).</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>String</code> containing <code>number</code> space characters.</p>
<h2 id="behavior-and-characteristics">Behavior and Characteristics</h2>
<h3 id="number-handling">Number Handling</h3>
<ul>
<li>If <code>number</code> = 0: Returns an empty string ("")</li>
<li>If <code>number</code> < 0: Generates a runtime error (Invalid procedure call or argument)</li>
<li>If <code>number</code> is a fractional value: Rounds to the nearest integer</li>
<li>Maximum value is limited by available memory</li>
</ul>
<h3 id="type-differences-space-vs-space">Type Differences: <code>Space$</code> vs <code>Space</code></h3>
<ul>
<li><code>Space$</code>: Always returns <code>String</code> type (never <code>Variant</code>)</li>
<li><code>Space</code>: Returns <code>Variant</code> containing a string</li>
<li>Use <code>Space$</code> when you need guaranteed <code>String</code> return type</li>
<li>Use <code>Space</code> when working with <code>Variant</code> variables</li>
</ul>
<h2 id="common-usage-patterns">Common Usage Patterns</h2>
<h3 id="1-padding-strings">1. Padding Strings</h3>
<pre><code class="language-vbnet">Function PadRight(text As String, width As Integer) As String
If Len(text) >= width Then
PadRight = text
Else
PadRight = text & Space$(width - Len(text))
End If
End Function
Dim padded As String
padded = PadRight("Hello", 10) ' "Hello "</code></pre>
<h3 id="2-creating-fixed-width-fields">2. Creating Fixed-Width Fields</h3>
<pre><code class="language-vbnet">Function FormatField(value As String, fieldWidth As Integer) As String
Dim temp As String
temp = value & Space$(fieldWidth)
FormatField = Left$(temp, fieldWidth)
End Function
Dim field As String
field = FormatField("Name", 20) ' "Name "</code></pre>
<h3 id="3-indentation">3. Indentation</h3>
<pre><code class="language-vbnet">Function IndentText(text As String, level As Integer) As String
IndentText = Space$(level * 4) & text
End Function
Debug.Print IndentText("Nested Item", 2) ' " Nested Item"</code></pre>
<h3 id="4-column-alignment-in-reports">4. Column Alignment in Reports</h3>
<pre><code class="language-vbnet">Sub PrintReport()
Dim col1 As String, col2 As String, col3 As String
col1 = "Name"
col2 = "Age"
col3 = "City"
Debug.Print col1 & Space$(15) & col2 & Space$(10) & col3
End Sub</code></pre>
<h3 id="5-creating-separator-lines">5. Creating Separator Lines</h3>
<pre><code class="language-vbnet">Function CreateSeparator(width As Integer, char As String) As String
' Create base with spaces then replace
CreateSeparator = String$(width, char)
End Function
' Or use spaces for visual separation
Debug.Print "Header" & Space$(10) & "Value"</code></pre>
<h3 id="6-text-centering">6. Text Centering</h3>
<pre><code class="language-vbnet">Function CenterText(text As String, width As Integer) As String
Dim padding As Integer
If Len(text) >= width Then
CenterText = Left$(text, width)
Else
padding = (width - Len(text)) \ 2
CenterText = Space$(padding) & text & Space$(width - Len(text) - padding)
End If
End Function
Dim centered As String
centered = CenterText("Title", 20)</code></pre>
<h3 id="7-creating-empty-buffers">7. Creating Empty Buffers</h3>
<pre><code class="language-vbnet">Function CreateBuffer(size As Integer) As String
CreateBuffer = Space$(size)
End Function
Dim buffer As String
buffer = CreateBuffer(100) ' 100-character buffer</code></pre>
<h3 id="8-formatting-tables">8. Formatting Tables</h3>
<pre><code class="language-vbnet">Sub PrintTableRow(col1 As String, col2 As String, col3 As String)
Dim row As String
row = Left$(col1 & Space$(20), 20) & _
Left$(col2 & Space$(15), 15) & _
Left$(col3 & Space$(10), 10)
Debug.Print row
End Sub</code></pre>
<h3 id="9-creating-blank-lines">9. Creating Blank Lines</h3>
<pre><code class="language-vbnet">Sub AddVerticalSpace(lines As Integer)
Dim i As Integer
For i = 1 To lines
Debug.Print Space$(0) ' Or just Debug.Print
Next i
End Sub</code></pre>
<h3 id="10-formatting-currency-values">10. Formatting Currency Values</h3>
<pre><code class="language-vbnet">Function FormatAmount(amount As Currency) As String
Dim amountStr As String
amountStr = Format$(amount, "#,##0.00")
FormatAmount = Space$(15 - Len(amountStr)) & amountStr
End Function
Debug.Print FormatAmount(1234.56) ' Right-aligned in 15 chars</code></pre>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Space()</code> - Returns a <code>Variant</code> containing the specified number of spaces</li>
<li><code>String$()</code> - Returns a string of repeating characters (more flexible than <code>Space$</code>)</li>
<li><code>Left$()</code> - Returns a specified number of characters from the left side</li>
<li><code>Right$()</code> - Returns a specified number of characters from the right side</li>
<li><code>Len()</code> - Returns the length of 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>
<li><code>Trim$()</code> - Removes both leading and trailing spaces</li>
<li><code>Spc()</code> - Used in <code>Print</code> statements to insert spaces</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="when-to-use-space-vs-string">When to Use <code>Space$</code> vs <code>String$</code></h3>
<pre><code class="language-vbnet">' Use Space$ for creating spaces specifically
Dim spaces As String
spaces = Space$(10) ' Clear intent
' Use String$ for other repeated characters
Dim dashes As String
dashes = String$(10, "-") ' More flexible
' Note: Space$(10) is equivalent to String$(10, " ")</code></pre>
<h3 id="prefer-constants-for-fixed-padding">Prefer Constants for Fixed Padding</h3>
<pre><code class="language-vbnet">' Less efficient: creating spaces repeatedly
For i = 1 To 1000
Debug.Print "Item" & Space$(10) & values(i)
Next i
' More efficient: create once
Const PADDING As String = " " ' 10 spaces
For i = 1 To 1000
Debug.Print "Item" & PADDING & values(i)
Next i</code></pre>
<h3 id="validate-negative-values">Validate Negative Values</h3>
<pre><code class="language-vbnet">Function SafeSpace(count As Integer) As String
If count < 0 Then
SafeSpace = ""
Else
SafeSpace = Space$(count)
End If
End Function</code></pre>
<h3 id="combine-with-left-or-right-for-fixed-width">Combine with <code>Left$</code> or <code>Right$</code> for Fixed Width</h3>
<pre><code class="language-vbnet">' Ensure exact width regardless of input length
Function FixedWidth(text As String, width As Integer) As String
Dim temp As String
temp = text & Space$(width)
FixedWidth = Left$(temp, width)
End Function</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Space$</code> is optimized for creating space-filled strings</li>
<li>Very efficient for small to moderate numbers of spaces (< 1000)</li>
<li>For large numbers, consider memory implications</li>
<li>Reuse space strings when possible instead of recreating</li>
</ul>
<pre><code class="language-vbnet">' Inefficient: creating space string in loop
For i = 1 To 10000
output = output & Space$(5) & data(i)
Next i
' More efficient: create once
Dim spacer As String
spacer = Space$(5)
For i = 1 To 10000
output = output & spacer & data(i)
Next i
' Even better: use array and Join
Dim parts() As String
ReDim parts(1 To 10000)
For i = 1 To 10000
parts(i) = spacer & data(i)
Next i
output = Join(parts, "")</code></pre>
<h2 id="common-pitfalls">Common Pitfalls</h2>
<h3 id="1-negative-values-cause-errors">1. Negative Values Cause Errors</h3>
<pre><code class="language-vbnet">' Runtime error: Invalid procedure call or argument
result = Space$(-5) ' ERROR!
' Always validate
If count >= 0 Then
result = Space$(count)
Else
result = ""
End If</code></pre>
<h3 id="2-confusion-with-spc-function">2. Confusion with <code>Spc()</code> Function</h3>
<pre><code class="language-vbnet">' Space$ returns a string
Dim s As String
s = Space$(5) ' Returns " " (5 spaces)
' Spc is used in Print statements only
Debug.Print "A"; Spc(5); "B" ' Prints "A B"
' Cannot assign Spc to variable
s = Spc(5) ' ERROR!</code></pre>
<h3 id="3-memory-issues-with-large-values">3. Memory Issues with Large Values</h3>
<pre><code class="language-vbnet">' Be careful with very large values
Dim huge As String
huge = Space$(10000000) ' 10 million spaces = ~20 MB
' Consider if you really need that many spaces
' Often there are better alternatives</code></pre>
<h3 id="4-not-accounting-for-existing-length">4. Not Accounting for Existing Length</h3>
<pre><code class="language-vbnet">' Wrong: may create string longer than desired width
result = text & Space$(width)
' Correct: ensure exact width
temp = text & Space$(width)
result = Left$(temp, width)</code></pre>
<h3 id="5-using-for-non-space-padding">5. Using for Non-Space Padding</h3>
<pre><code class="language-vbnet">' Wrong: Space$ only creates spaces
underline = Space$(20) ' Trying to create underline
' This creates " ", not "____________________"
' Correct: use String$ for other characters
underline = String$(20, "_")</code></pre>
<h3 id="6-floating-point-rounding">6. Floating Point Rounding</h3>
<pre><code class="language-vbnet">Debug.Print Space$(5.4) ' Creates 5 spaces (rounds down)
Debug.Print Space$(5.6) ' Creates 6 spaces (rounds up)
Debug.Print Space$(5.5) ' Creates 6 spaces (banker's rounding)
' Be explicit with integer conversion if needed
Dim count As Integer
count = Int(5.7) ' Force truncation
result = Space$(count)</code></pre>
<h2 id="practical-examples">Practical Examples</h2>
<h3 id="creating-fixed-width-reports">Creating Fixed-Width Reports</h3>
<pre><code class="language-vbnet">Sub PrintInvoice()
Dim header As String
Dim separator As String
' Create header with aligned columns
header = Left$("Item" & Space$(30), 30) & _
Left$("Qty" & Space$(10), 10) & _
Left$("Price" & Space$(15), 15)
separator = String$(55, "-")
Debug.Print header
Debug.Print separator
Debug.Print Left$("Widget" & Space$(30), 30) & _
Right$(Space$(10) & "5", 10) & _
Right$(Space$(15) & "$10.00", 15)
End Sub</code></pre>
<h3 id="building-hierarchical-output">Building Hierarchical Output</h3>
<pre><code class="language-vbnet">Sub PrintTree(text As String, level As Integer)
Debug.Print Space$(level * 2) & "- " & text
End Sub
PrintTree("Root", 0)
PrintTree("Child 1", 1)
PrintTree("Grandchild", 2)
PrintTree("Child 2", 1)</code></pre>
<h3 id="formatting-data-for-fixed-width-files">Formatting Data for Fixed-Width Files</h3>
<pre><code class="language-vbnet">Function FormatRecord(name As String, age As Integer, city As String) As String
Dim record As String
record = Left$(name & Space$(25), 25) & _
Right$(Space$(3) & CStr(age), 3) & _
Left$(city & Space$(20), 20)
FormatRecord = record
End Function
' Writes: "John Doe 25New York "
Print #1, FormatRecord("John Doe", 25, "New York")</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Can only create space characters (ASCII 32), not other whitespace</li>
<li>Negative values cause runtime errors</li>
<li>Very large values can cause out-of-memory errors</li>
<li>Cannot be used directly in <code>Print</code> statements like <code>Spc()</code></li>
<li>Floating-point parameters are rounded (may be unexpected)</li>
<li>Maximum string length limited by VB6 string constraints (~2 GB)</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>