VB6Parse / Library / String / ltrim

VB6 Library Reference

LTrim Function

Returns a String containing a copy of a specified string with leading spaces removed.

Syntax

LTrim(string)

Parameters

Return Value

Returns a String (or Variant): - Copy of string with leading spaces removed - Removes only spaces (ASCII 32) from the left - 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 - Trailing spaces are preserved - Internal spaces are preserved

Remarks

The LTrim function removes leading spaces:

Typical Uses

  1. Remove Leading Spaces
cleanText = LTrim("   Hello")
  1. Clean User Input
userName = LTrim(txtUsername.Text)
  1. Process Fixed-Width Data
field = LTrim(Mid(line, 1, 20))
  1. Normalize Text
normalizedText = LTrim(RTrim(inputText))
  1. Data Import Cleanup
value = LTrim(csvField)
  1. Remove Padding
If LTrim(textBox.Text) = "" Then
MsgBox "Required field"
End If
  1. Format Display
lblName.Caption = LTrim(recordset("Name"))
  1. Conditional Processing
If LTrim(line) <> "" Then
ProcessLine line
End If

Basic Examples

Example 1: Basic Usage

Dim result As String

result = LTrim("   Hello")           ' Returns "Hello"
result = LTrim("Hello   ")           ' Returns "Hello   " (trailing preserved)
result = LTrim("   Hello World   ")  ' Returns "Hello World   "
result = LTrim("NoSpaces")           ' Returns "NoSpaces"
result = LTrim("     ")              ' Returns ""
result = LTrim("")                   ' Returns ""

Example 2: Clean User Input

Private Sub txtUsername_LostFocus()
' Remove leading spaces from input
txtUsername.Text = LTrim(txtUsername.Text)

' Validate
If LTrim(txtUsername.Text) = "" Then
MsgBox "Username is required", vbExclamation
txtUsername.SetFocus
End If
End Sub

Example 3: Process Fixed-Width File

Sub ProcessFixedWidthFile(ByVal filename As String)
Dim fileNum As Integer
Dim line As String
Dim firstName As String
Dim lastName As String

fileNum = FreeFile
Open filename For Input As #fileNum

Do While Not EOF(fileNum)
Line Input #fileNum, line

' Extract fields (positions 1-20 and 21-40)
firstName = LTrim(Mid(line, 1, 20))
lastName = LTrim(Mid(line, 21, 20))

Debug.Print firstName & " " & lastName
Loop

Close #fileNum
End Sub

Example 4: Text Normalization

Function NormalizeText(ByVal text As String) As String
' Remove leading and trailing spaces
NormalizeText = LTrim(RTrim(text))

' Could also use: NormalizeText = Trim(text)
End Function

' Usage
Dim clean As String
clean = NormalizeText("   Hello World   ")  ' Returns "Hello World"

Common Patterns

Pattern 1: FullTrim (combine with RTrim)

Function FullTrim(ByVal text As String) As String
FullTrim = LTrim(RTrim(text))
' Note: Can also use built-in Trim() function
End Function

Pattern 2: IsBlank (check for empty or whitespace)

Function IsBlank(ByVal text As String) As Boolean
IsBlank = (LTrim(RTrim(text)) = "")
End Function

Pattern 3: SafeLTrim (handle Null)

Function SafeLTrim(ByVal text As Variant) As String
If IsNull(text) Then
SafeLTrim = ""
Else
SafeLTrim = LTrim(text)
End If
End Function

Pattern 4: CleanInput

Function CleanInput(ByVal userInput As String) As String
' Remove leading/trailing spaces and convert to proper case
CleanInput = LTrim(RTrim(userInput))
If CleanInput <> "" Then
CleanInput = UCase(Left(CleanInput, 1)) & LCase(Mid(CleanInput, 2))
End If
End Function

Pattern 5: TrimFields (process array)

Sub TrimFields(fields() As String)
Dim i As Integer
For i = LBound(fields) To UBound(fields)
fields(i) = LTrim(RTrim(fields(i)))
Next i
End Sub

Pattern 6: ParsePaddedValue

Function ParsePaddedValue(ByVal paddedText As String) As String
' Remove leading spaces from fixed-width field
ParsePaddedValue = LTrim(paddedText)
End Function

Pattern 7: ValidateRequired

Function ValidateRequired(ByVal fieldValue As String, _
ByVal fieldName As String) As Boolean
If LTrim(RTrim(fieldValue)) = "" Then
MsgBox fieldName & " is required", vbExclamation
ValidateRequired = False
Else
ValidateRequired = True
End If
End Function

Pattern 8: TrimAllControls

Sub TrimAllControls(ByVal frm As Form)
Dim ctrl As Control

For Each ctrl In frm.Controls
If TypeOf ctrl Is TextBox Then
ctrl.Text = LTrim(RTrim(ctrl.Text))
End If
Next ctrl
End Sub

Pattern 9: ParseCSVField

Function ParseCSVField(ByVal field As String) As String
' Remove quotes and trim
If Left(field, 1) = """" And Right(field, 1) = """" Then
field = Mid(field, 2, Len(field) - 2)
End If
ParseCSVField = LTrim(RTrim(field))
End Function

Pattern 10: RemoveLeadingSpaces

Sub RemoveLeadingSpaces(ByVal textBox As TextBox)
Dim selStart As Long
selStart = textBox.SelStart
textBox.Text = LTrim(textBox.Text)
textBox.SelStart = selStart
End Sub

Advanced Examples

Example 1: Data Import Processor

' Class: DataImporter
Private m_data As Collection

Public Sub ImportFixedWidthFile(ByVal filename As String)
Dim fileNum As Integer
Dim line As String
Dim record As Dictionary

Set m_data = New Collection

fileNum = FreeFile
Open filename For Input As #fileNum

Do While Not EOF(fileNum)
Line Input #fileNum, line

If Len(line) >= 60 Then
Set record = New Dictionary

' Extract and trim fields
record("ID") = LTrim(Mid(line, 1, 10))
record("Name") = LTrim(Mid(line, 11, 30))
record("City") = LTrim(Mid(line, 41, 20))

m_data.Add record
End If
Loop

Close #fileNum
End Sub

Public Property Get RecordCount() As Long
RecordCount = m_data.Count
End Property

Public Function GetRecord(ByVal index As Long) As Dictionary
Set GetRecord = m_data(index)
End Function

Example 2: Text Field Validator

' Class: TextValidator
Private m_errors As Collection

Public Sub ValidateForm(ByVal frm As Form)
Set m_errors = New Collection

Dim ctrl As Control
For Each ctrl In frm.Controls
If TypeOf ctrl Is TextBox Then
ValidateTextBox ctrl
End If
Next ctrl
End Sub

Private Sub ValidateTextBox(ByVal txt As TextBox)
Dim trimmed As String
trimmed = LTrim(RTrim(txt.Text))

' Check if required (assuming Tag property indicates required)
If txt.Tag = "Required" Then
If trimmed = "" Then
m_errors.Add "Field '" & txt.Name & "' is required"
End If
End If

' Check minimum length
If txt.Tag Like "MinLen:*" Then
Dim minLen As Integer
minLen = Val(Mid(txt.Tag, 8))

If Len(trimmed) < minLen Then
m_errors.Add "Field '" & txt.Name & "' must be at least " & _
minLen & " characters"
End If
End If
End Sub

Public Property Get IsValid() As Boolean
IsValid = (m_errors.Count = 0)
End Property

Public Property Get Errors() As Collection
Set Errors = m_errors
End Property

Example 3: String Utilities Module

' Module: StringUtils

Public Function TrimAll(ByVal text As String) As String
TrimAll = LTrim(RTrim(text))
End Function

Public Function IsNullOrWhitespace(ByVal text As Variant) As Boolean
If IsNull(text) Then
IsNullOrWhitespace = True
ElseIf VarType(text) = vbString Then
IsNullOrWhitespace = (LTrim(RTrim(text)) = "")
Else
IsNullOrWhitespace = False
End If
End Function

Public Function NormalizeSpaces(ByVal text As String) As String
Dim result As String
Dim i As Integer
Dim lastWasSpace As Boolean

' Remove leading spaces
text = LTrim(text)

' Collapse multiple spaces to single space
For i = 1 To Len(text)
If Mid(text, i, 1) = " " Then
If Not lastWasSpace Then
result = result & " "
lastWasSpace = True
End If
Else
result = result & Mid(text, i, 1)
lastWasSpace = False
End If
Next i

NormalizeSpaces = RTrim(result)
End Function

Public Function CleanTextArray(textArray() As String) As String()
Dim i As Integer
Dim result() As String

ReDim result(LBound(textArray) To UBound(textArray))

For i = LBound(textArray) To UBound(textArray)
result(i) = LTrim(RTrim(textArray(i)))
Next i

CleanTextArray = result
End Function

Example 4: Form Input Manager

' Class: FormInputManager
Private m_form As Form

Public Sub AttachToForm(ByVal frm As Form)
Set m_form = frm
End Sub

Public Sub TrimAllInputs()
Dim ctrl As Control

For Each ctrl In m_form.Controls
If TypeOf ctrl Is TextBox Then
ctrl.Text = LTrim(RTrim(ctrl.Text))
ElseIf TypeOf ctrl Is ComboBox Then
ctrl.Text = LTrim(RTrim(ctrl.Text))
End If
Next ctrl
End Sub

Public Function ValidateRequired() As Boolean
Dim ctrl As Control
Dim trimmed As String
Dim isValid As Boolean

isValid = True

For Each ctrl In m_form.Controls
If TypeOf ctrl Is TextBox Then
If ctrl.Tag = "Required" Then
trimmed = LTrim(RTrim(ctrl.Text))

If trimmed = "" Then
MsgBox "Field is required: " & ctrl.Name, vbExclamation
ctrl.SetFocus
isValid = False
Exit For
End If
End If
End If
Next ctrl

ValidateRequired = isValid
End Function

Public Function GetCleanValue(ByVal controlName As String) As String
Dim ctrl As Control

On Error Resume Next
Set ctrl = m_form.Controls(controlName)

If Not ctrl Is Nothing Then
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
GetCleanValue = LTrim(RTrim(ctrl.Text))
End If
End If
End Function

Error Handling

' LTrim handles Null gracefully
Dim result As Variant
result = LTrim(Null)  ' Returns Null

' Safe trimming with Null check
Function SafeTrim(ByVal value As Variant) As String
If IsNull(value) Then
SafeTrim = ""
Else
SafeTrim = LTrim(RTrim(CStr(value)))
End If
End Function

Performance Considerations

Best Practices

  1. Use Trim() for both sides - More efficient than LTrim(RTrim())
  2. Validate before use - Check for Null if using Variant
  3. Clean user input early - Trim in validation routines
  4. Cache trimmed values - Don't call repeatedly in loops
  5. Document expectations - Clarify if tabs/newlines should be removed
  6. Use with database fields - Clean imported data
  7. Combine with validation - Check for empty after trimming
  8. Apply to all text inputs - Standardize data entry
  9. Consider Unicode - LTrim only removes ASCII space (32)
  10. Test edge cases - Empty strings, all spaces, Null values
Function Removes Leading Removes Trailing Removes Both
LTrim Yes No No
RTrim No Yes No
Trim Yes Yes Yes

LTrim vs RTrim vs Trim

Dim text As String
text = "   Hello World   "

' LTrim - removes leading spaces only
Debug.Print "[" & LTrim(text) & "]"   ' [Hello World   ]

' RTrim - removes trailing spaces only
Debug.Print "[" & RTrim(text) & "]"   ' [   Hello World]

' Trim - removes both leading and trailing
Debug.Print "[" & Trim(text) & "]"    ' [Hello World]

' Manual equivalent to Trim
Debug.Print "[" & LTrim(RTrim(text)) & "]"  ' [Hello World]

Whitespace Characters

' LTrim only removes space (ASCII 32)
Dim text As String

text = "   Hello"        ' Spaces - REMOVED
text = Chr(9) & "Hello"  ' Tab - NOT REMOVED
text = Chr(10) & "Hello" ' Line feed - NOT REMOVED
text = Chr(13) & "Hello" ' Carriage return - NOT REMOVED
text = Chr(160) & "Hello" ' Non-breaking space - NOT REMOVED

' To remove other whitespace, use custom function
Function TrimAllWhitespace(ByVal text As String) As String
Do While Len(text) > 0
Dim ch As String
ch = Left(text, 1)

If ch = " " Or ch = Chr(9) Or ch = Chr(10) Or ch = Chr(13) Then
text = Mid(text, 2)
Else
Exit Do
End If
Loop

TrimAllWhitespace = text
End Function

Platform Notes

Limitations

← Back to String | View all functions