Trim - VBScript


Overview

The Trim() function eliminates leading and trailing whitespace (spaces, tabs, newlines) from a string, making it useful for sanitizing input and ensuring consistent data handling.

Syntax

Trim(string)

Options/Flags

N/A

Examples

Remove leading and trailing whitespace:

Dim str = "   Hello, world!   "
str = Trim(str)
WScript.Echo str ' Output: Hello, world!

Sanitize user input:

Dim input = InputBox("Enter your name:")
input = Trim(input) ' Remove any extra whitespace

Common Issues

Avoid multiple leading/trailing whitespace: Ensure Trim() is used only once, as it may unintentionally remove multiple spaces.

Integration

  • Combine with Replace() to remove specific characters or strings.
  • Use Split() to create an array from a string with whitespace as the delimiter, then Trim() each array element.
  • Replace()
  • Split()
  • StrCompare()