Left$ Function
Returns a String containing a specified number of characters from the left side of a string.
Syntax
Left$(string, length)
Parameters
string: Required. String expression from which the leftmost characters are returned. IfstringcontainsNull,Nullis returned.length: Required. Numeric expression indicating how many characters to return. If 0, a zero-length string ("") is returned. If greater than or equal to the number of characters instring, the entire string is returned.
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
Related Functions
Left: Variant version that returns aVariantRight$: Returns characters from the right side of a stringMid$: Returns characters from the middle of a stringLen: Returns the length of a stringInStr: Finds the position of a substringLTrim$: Removes leading spaces from a stringTrim$: Removes leading and trailing spaces
Best Practices
- Always validate that
lengthis not negative before calling - Use
Lento check string length before extracting - Handle empty strings appropriately in your logic
- Consider using
InStrwithLeft$for dynamic parsing - Remember that
Left$(str, 0)returns an empty string - Use
Left$instead ofLeftwhen you need aStringtype explicitly - Combine with
Trim$when dealing with user input - Be aware that requesting more characters than exist returns the full string
- Use comparison with
Left$for prefix checking (faster thanInStr) - Cache the result if using the same
Left$call multiple times
Performance Considerations
Left$is a very fast operation in VB6- More efficient than using
Mid$for extracting from the beginning - Faster than string concatenation for prefix operations
- No performance penalty for requesting more characters than available
- Using
Left$for prefix comparison is faster than regular expressions
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
- Passing negative length values (causes runtime error)
- Assuming
Left$will throw an error if length exceeds string length (it doesn't) - Not handling
Nullstrings (causes runtime error) - Confusing zero-based vs one-based indexing (VB6 strings are 1-based)
- Using
Left$on binary data (useLeftB$instead) - Forgetting that the length parameter is character count, not position
- Not trimming strings before extraction (may get unwanted spaces)
Limitations
- Cannot extract from right side (use
Right$instead) - Cannot specify starting position (use
Mid$instead) - Does not work with byte arrays directly
- No built-in support for Unicode surrogate pairs
- Length parameter cannot be an expression that evaluates to
Null - Returns
Nullif the string argument isNull