VB6Parse / Library / String / left_dollar

VB6 Library Reference

Left$ Function

Returns a String containing a specified number of characters from the left side of a string.

Syntax

Left$(string, length)

Parameters

Return Value

Returns a String containing the leftmost length characters from string. If length is 0, returns an empty string. If length is greater than or equal to the length of string, returns the entire string.

Remarks

The Left$ function returns the specified number of characters from the left (beginning) of a string. It's commonly used for string parsing, extracting prefixes, or taking substrings from the start of a string.

To determine the number of characters in string, use the Len function.

Left$ is the string-specific version that always returns a String. The Left function returns a Variant.

Typical Uses

Example 1: Extracting File Extension Prefix

Dim filename As String
filename = "document.txt"
prefix = Left$(filename, 3)  ' "doc"

Example 2: Getting First Characters

Dim text As String
text = "Hello, World!"
greeting = Left$(text, 5)  ' "Hello"

Example 3: Extracting Area Code

Dim phone As String
phone = "5551234567"
areaCode = Left$(phone, 3)  ' "555"

Example 4: Getting Date Components

Dim dateStr As String
dateStr = "2024-01-15"
year = Left$(dateStr, 4)  ' "2024"

Common Usage Patterns

Checking String Prefix

If Left$(filename, 4) = "tmp_" Then
Debug.Print "Temporary file"
End If

Extracting Initials

Dim name As String
name = "John Doe"
initial = Left$(name, 1)  ' "J"

Parsing Fixed-Width Data

Dim record As String
record = "12345John     Smith    "
id = Left$(record, 5)  ' "12345"

Truncating Long Strings

Dim description As String
description = "Very long description text..."
If Len(description) > 50 Then
description = Left$(description, 47) & "..."
End If

Extracting Drive Letter

Dim path As String
path = "C:\Windows\System32"
drive = Left$(path, 1)  ' "C"

Getting Protocol from URL

Dim url As String
url = "https://example.com"
protocol = Left$(url, 5)  ' "https"

Validating File Type

Dim fileName As String
fileName = "IMG_1234.JPG"
If Left$(fileName, 4) = "IMG_" Then
processImage fileName
End If

Extracting Country Code

Dim phoneNumber As String
phoneNumber = "+1-555-1234"
If Left$(phoneNumber, 1) = "+" Then
countryCode = Left$(phoneNumber, 2)  ' "+1"
End If

Creating Abbreviations

Dim state As String
state = "California"
abbr = UCase$(Left$(state, 2))  ' "CA"

Parsing CSV First Field

Dim csvLine As String
csvLine = "John,Doe,555-1234"
Dim pos As Integer
pos = InStr(csvLine, ",")
If pos > 0 Then
firstName = Left$(csvLine, pos - 1)  ' "John"
End If

Best Practices

  1. Always validate that length is not negative before calling
  2. Use Len to check string length before extracting
  3. Handle empty strings appropriately in your logic
  4. Consider using InStr with Left$ for dynamic parsing
  5. Remember that Left$(str, 0) returns an empty string
  6. Use Left$ instead of Left when you need a String type explicitly
  7. Combine with Trim$ when dealing with user input
  8. Be aware that requesting more characters than exist returns the full string
  9. Use comparison with Left$ for prefix checking (faster than InStr)
  10. Cache the result if using the same Left$ call multiple times

Performance Considerations

String Indexing

Length Value Result
0 Returns empty string ("")
1 to Len(string) Returns that many characters from left
> Len(string) Returns entire string
Negative Runtime error (Invalid procedure call or argument)

Common Pitfalls

Limitations

← Back to String | View all functions