VBScript offers a number of functions for manipulating text strings. We’re going to cover the syntax of these functions below and provide examples of how they can be used.
If you know specifically which string function you are looking for, click its name below and you will jump straight to that example:
- CStr (convert a value to a string)
- InStr (is one string found inside another)
- InStrRev (starting at the end of the string, is one string found inside another)
- LCase (convert the string to all lowercase)
- Left (retrieve a number of characters from the left or start of the string)
- Len (return the length of the entire string)
- LTrim (Remove spaces from the start of the string)
- Mid (Return a specific number of characters from within the string)
- Replace (Replace a specific set of characters in the string with a different set of characters)
- Right (return a number of characters from the end of the string)
- RTrim (Remove spaces from the end of the string)
- Space (return a string consisting of a specific number of spaces)
- StrComp (compare two strings and return a result)
- String (return a string that contains a specific number of a single character)
- StrReverse (reverse the contents of a string)
- Trim (Removes from both ends of the string)
- UCase (convert the string to uppercase)
Detailed information appears for each function.
CStr – Convert this value to a string
Syntax:
CStr(value)
Parameters:
- value = the value you want to convert to a string
Notes:
- A boolean value will be converted into a string containing "true" or "false"
- A date value will be converted to a string in short date format
- A null value will cause an error to occur
- An empty value will cause an empty string (e.g., "") to be returned
- An error object will return the value "error" followed by a number
- A numeric value will be converted to a string with the number
Usage Example:
theDate = now
dateString = CStr(theDate)
wscript.echo "The Date is: " & dateString
In the above example, the script will return the current date as a string which look something like "4/10/2011 1:48:53 AM".
InStr – "Is this substring found within the string?"
Syntax:
InStr(start,string,substring,howToCompare)
Parameters:
- start = the starting position of the string from which you want to begin searching (e.g., 1 means you want to start from the first character in the string, 9 would mean that you want to skip past the first 8 characters)
- string = the string you want to search inside
- substring = the substring you want to find inside the main string
- howToCompare = the type of comparison to perform (0=binary, 1=text) [This parameter is optional. If left out, a textual comparison is assumed.]
Usage Example:
bigString = "The quick brown fox jumped over the lazy dog " &_
"and then went to eat a pizza."
subString = "dog"
dogIndex = InStr(1,bigString,subString,1)
wscript.echo "The word '" & subString & "' appears at " & _
"character " & dogIndex & " in the string."
In the above example, the script will return the message "The word ‘dog’ appears at character 42 in the string."
InStrRev – "Is this substring found within the string, starting at the end?"
Syntax:
InStrRev(string,substring,start,howToCompare)
Parameters:
- start = the starting position of the string from which you want to begin searching (e.g., 1 means you want to start from the first character in the string, 9 would mean that you want to skip past the first 8 characters) [This parameter is optional. If left out, a textual comparison is assumed.]
- string = the string you want to search inside
- substring = the substring you want to find inside the main string
- howToCompare = the type of comparison to perform (0=binary, 1=text) [This parameter is optional. If left out, a textual comparison is assumed.]
Usage Example:
bigString = "The quick brown fox jumped over the lazy dog " &_
"and then went to eat a pizza."
subString = "dog"
dogIndex = InStrRev(bigString,subString)
wscript.echo "The word '" & subString & "' appears at " & _
"character " & dogIndex & " in the string."
In the above example, the script will return the message "The word ‘dog’ appears at character 42 in the string."
LCase – "Convert this string to all lowercase characters"
Syntax:
LCase(string)
Parameters:
- string = the string you want to convert to lowercase
Usage Example:
mixedString = "MiCroSoFt WINDOWs"
resultString = LCase(mixedString)
wscript.echo mixedString & " becomes " & resultString &_
" when converted to lowercase"
In the above example, the script will return the message "MiCroSoFt WINDOWs becomes microsoft windows when converted to lowercase."
Left – Return the leftmost characters of the string
Syntax:
Left(string,numberOfCharacters)
Parameters:
- string = the string you want to extract characters from
- numberOfCharacters = the number of characters you want to return from the string
Usage Example:
theString = "Windows"
resultString = Left(theString,3)
wscript.echo "The three leftmost characters of '" & theString & "' " &_
"are '" & resultString & "'."
In the above example, the script will return the message "The three leftmost characters of ‘Windows’ are ‘Win’."
Len – "How Long is this String?"
Syntax:
Len(string)
Parameters:
- string = the string for which you’d like to know the length
Usage Example:
theString = "Microsoft Windows"
characterCount = Len(theString)
wscript.echo "The string '" & theString & "' is " & characterCount &_
" characters long."
In the above example, the script will return the message "The string ‘Microsoft Windows’ is 17 characters long."
LTrim – Trim the spaces off the left-hand side, or start, of the string
Syntax:
LTrim(string)
Parameters:
- string = the string you would like to trim spaces from
Usage Example:
theString = " Microsoft Windows "
modifiedString = LTrim(theString)
wscript.echo "When we remove all spaces from the left of '" &_
theString & "', we get '" & modifiedString & "'."
In the above example, the script will return the message "When we remove all spaces from the left of ‘ Microsoft Windows’, we get ‘Microsoft Windows ‘."
Mid – "Give me x number of characters from the string, starting at position y"
Syntax:
Mid(string,start,length)
Parameters:
- string = the string you would like to extract characters from
- start = the position in the string where the extract starts
- length = the number of characters you want to extract
Usage Example:
theString = "This is an example string." sampleString = mid(theString,12,7) wscript.echo "Characters 12 through 19 are: '" & sampleString & "'"
In the above example, the script will return the message "Characters 12 through 19 are: ‘example’".
Replace – "Replace these characters in the string with these others"
Syntax:
Replace(string,substrToFind,substrToReplWith,startChar,count,compare)
Parameters:
- string = the string you would like to replace characters in
- substrToFind = the substring you want to find within the string
- substrToReplWith = the substring you want to substitute or replace for the one you find
- startChar = the position in the string where you want to start replacing (This is an optional value. If omitted, the start of the string is assumed.) Note that if you specify a value greater than 1, all characters in the string prior to that position are removed from the string.)
- count = the number of replacements you want to make (This is an optional value. If omitted, all occurrences of substrToFind will be replaced with substrToReplWith.)
- compare = the type of string comparison to make (0 is a binary compare, 1 is a textual comparison, with 0 being the default – this argument is optional)
Usage Example:
theString = "This is a word followed by a word and another word."
newWord = "phrase"
newString = Left(theString,15) & Replace(theString,"word",newWord,15,1,0)
wscript.echo newString
In the above example, the script will return the message "This is a word followed by a phrase and another word."
Right – Return the rightmost characters of the string
Syntax:
Right(string,numberOfCharacters)
Parameters:
- string = the string you want to extract characters from
- numberOfCharacters = the number of characters you want to return from the end of the string
Usage Example:
theString = "Windows"
resultString = Right(theString,3)
wscript.echo "The three rightmost characters of '" & theString & "' " &_
"are '" & resultString & "'."
In the above example, the script will return the message "The three rightmost characters of ‘Windows’ are ‘ows’."
RTrim – Trim the spaces off the right-hand side, or end, of the string
Syntax:
RTrim(string)
Parameters:
- string = the string you would like to trim spaces from
Usage Example:
theString = "This is an example string that ends with 12 spaces. " sampleString = Rtrim(theString) wscript.echo "When the string is trimmed, we get:" & vbcrlf & "'" & theString & "'"
In the above example, the script will return the message "When we remove all spaces from the left of ‘ Microsoft Windows ‘, we get ‘ Microsoft Windows’."
Space – Return a string of x number of spaces
Syntax:
Space(numberOfSpaces)
Parameters:
- numberOfSpaces = the number of spaces you want in the resulting string
Usage Example:
sampleString = Spaces(40)
wscript.echo "There are 40 spaces between these " &_
"two apostrophes: '" & sampleString & "'"
In the above example, the script will return the message "There are 40 spaces between these two apostrophes: ‘ ‘".
StrComp – Compare these two strings
Syntax:
StrComp(string1,string2,compare)
Parameters:
- string1 = the first string you want to compare
- string2 = the second string to be compared
- compare = the type of comparison to apply to the two strings, 0 (vbBinaryCompare) for a binary comparison or 1 (vbTextCompare) for a textual comparison (This is an optional parameter. The default is 0.)
Usage Example:
stringa = "Apple"
stringb = "Ball"
stringc = "Cat"
wscript.echo "Comparing '" & stringa & "' to '" & stringa & "' yields: " &_
StrComp(stringa,stringa)
wscript.echo "Comparing '" & stringa & "' to '" & stringb & "' yields: " &_
StrComp(stringa,stringb)
wscript.echo "Comparing '" & stringc & "' to '" & stringb & "' yields: " &_
StrComp(stringc,stringb)
wscript.echo "Comparing '" & stringc & "' to '" & stringa & "' yields: " &_
StrComp(stringc,stringa)
In the above example, the script will produce four lines of output showing the values you get by comparing the three strings different ways:
Comparing 'Apple' to 'Apple' yields: 0
Comparing 'Apple' to 'Ball' yields: –1
Comparing 'Cat' to 'Ball' yields: 1 Comparing 'Cat' to 'Apple' yields: 1
As you may have guessed, when the first string is the same as the second, this function returns a zero value. When the first string is alphabetically "before" the second, StrComp returns "-1". When the first string is alphabetically "after" the second, StrComp returns "1".
String – Return a string of x number of a specific character
Syntax:
String(numberOfCharacters,characterToRepeat)
Parameters:
- numberOfSpaces = the number of spaces you want in the resulting string
- characterToRepeat = the character you want to repeat in the string
Usage Example:
sampleString = String(40,"*")
wscript.echo sampleString
In the above example, the script will output a string of 40 asterisks ("*").
StrReverse – Reverse the order of the characters in the string
Syntax:
StrReverse(string)
Parameters:
- string = the string whose characters you want to reverse
Usage Example:
sampleString = "!em esreveR"
wscript.echo StrReverse(sampleString)
In the above example, the script will output the phrase "Reverse me!".
Trim – Remove spaces from the start and end of the string
Syntax:
Trim(string)
Parameters:
- string = the string you would like beginning and ending spaces removed from
Usage Example:
sampleString = " No Spaces! "
wscript.echo Trim(sampleString)
In the above example, the script will output the phrase "No Spaces!" without any preceding or trailing spaces.
UCase – Convert the string to uppercase
Syntax:
UCase(string)
Parameters:
- string = the string whose characters you want to convert to uppercase
Usage Example:
sampleString = "MiXed Case example"
wscript.echo Ucase(sampleString)
In the above example, the script will output the phrase "MIXED CASE EXAMPLE".
Topics and Keywords Discussed on this Page which may be relevant to Internet searchers, to help them find this page:
- String manipulation in VBScript
- VBScript string functions
- VBScript string usage
- Replace characters in a string
- Remove characters from a string
- Delete spaces at the start of a string
- VBScript string commands
- Compare strings
- Delete spaces at the end of a string
- Remove illegal characters from a string
- VBScript string data manipulation
- Manipulating strings in VBScript
- working with strings in VBScript
- using strings
- string modification
- adjust strings
- change case in a string
- make a string uppercase
- make a string lowercase
- VBScript string statements
- VBScript string functions
