VBScript Strings
last modified February 19, 2025
In this article, we will learn how to work with strings in VBScript. Strings are
sequences of characters used to represent text. We will use WScript.Echo
to output results and run the scripts using cscript.
Hello message
The first example demonstrates how to create and display a string.
Dim message message = "Hello there!" WScript.Echo message
This example declares a string variable message and assigns it the
value "Hello there!". The string is then displayed using WScript.Echo.
Concatenating Strings
You can concatenate strings using the & operator.
Dim firstName, lastName, fullName firstName = "John" lastName = "Doe" fullName = firstName & " " & lastName WScript.Echo fullName
This example concatenates two strings firstName and lastName
to create a full name.
String Length
You can determine the length of a string using the Len function.
Dim text text = "VBScript" WScript.Echo "Length: " & Len(text)
This example calculates and displays the length of the string text.
Substrings
You can extract a substring from a string using the Mid function.
Dim sentence, substring sentence = "The quick brown fox" substring = Mid(sentence, 5, 5) WScript.Echo "Substring: " & substring
This example extracts a substring starting at position 5 with a length of 5 characters.
String Comparison
You can compare strings using the = operator or the StrComp
function.
Dim str1, str2
str1 = "apple"
str2 = "Apple"
If StrComp(str1, str2, vbTextCompare) = 0 Then
WScript.Echo "Strings are equal."
Else
WScript.Echo "Strings are not equal."
End If
This example compares two strings case-insensitively using StrComp.
String Replacement
You can replace parts of a string using the Replace function.
Dim original, replaced original = "I like apples." replaced = Replace(original, "apples", "oranges") WScript.Echo replaced
This example replaces the word "apples" with "oranges" in the string
original.
String Splitting
You can split a string into an array using the Split function.
Dim data, parts, part
data = "apple,banana,cherry"
parts = Split(data, ",")
For Each part In parts
WScript.Echo part
Next
This example splits the string data into an array using a comma as
the delimiter and outputs each part.
String Trimming
You can remove leading and trailing spaces from a string using the Trim,
LTrim, and RTrim functions.
Dim untrimmed, trimmed untrimmed = " VBScript " trimmed = Trim(untrimmed) WScript.Echo "Trimmed: '" & trimmed & "'"
This example trims leading and trailing spaces from the string untrimmed.
String Case Conversion
You can convert a string to uppercase or lowercase using the UCase
and LCase functions.
Dim mixedCase, upperCase, lowerCase mixedCase = "VBScript" upperCase = UCase(mixedCase) lowerCase = LCase(mixedCase) WScript.Echo "Uppercase: " & upperCase WScript.Echo "Lowercase: " & lowerCase
This example converts the string mixedCase to uppercase and lowercase.
String Searching
You can search for a substring within a string using the InStr
function.
Dim mainString, searchString, position mainString = "The quick brown fox" searchString = "brown" position = InStr(mainString, searchString) WScript.Echo "Position: " & position
This example searches for the substring "brown" within the string
mainString and returns its position.
In this article, we explored how to work with strings in VBScript. We covered creating strings, concatenation, length, substrings, comparison, replacement, splitting, trimming, case conversion, and searching. Strings are fundamental to many programming tasks, and VBScript provides a rich set of functions to manipulate them.
Author
List all VBScript tutorials.