Len - VBScript


Overview

The Len command in VB Script returns the number of characters in a string variable. It is used to determine the length of a string and is particularly useful for manipulating and comparing strings.

Syntax

Len(string)

Parameters:

  • string: The string to measure the length of.

Options/Flags

None.

Examples

' Get the length of a string
Dim myString = "Hello World"
Dim myStringLength = Len(myString)
MsgBox myStringLength  ' Output: 11

' Compare the lengths of two strings
If Len("Apple") > Len("Banana") Then
    MsgBox "Apple is shorter than Banana"
End If

Common Issues

  • Unexpected length: Ensure that the string you are providing is valid and not empty.
  • Negative length: Len does not return negative values, even for empty strings.

Integration

Len can be integrated with other VB Script commands for advanced string manipulation tasks. For example, you can use Len together with Mid to extract a substring of a specific length.

Dim longText = "Lorem ipsum dolor sit amet"
Dim shortText = Mid(longText, 1, Len(longText) - 5)
  • Instr – Finds the position of a substring within a string.
  • Trim – Removes leading and trailing spaces from a string.
  • Replace – Replaces occurrences of a substring with another.