| << D.5.0- Unsupported Array functions and Statements | AppendixD | D.6.0- Unsupported String functions, statements and constructs >> |
String Functions and Statements
Whatever your application does, you are likely to use string manipulation. By string manipulation we mean things like extracting a name from a string, checking if a particular string is part of another string, formatting numbers as strings with delimiters, and so on. Below is a list of the various string functions in VBScript.
Some functionality is not exposed as functions, but as methods of objects. For
Example, the RegExp object exposes regular
expression support. See chapter
10 The Scripting Objects.
|
Format |
|
|
Syntax
|
FormatCurrency(expression [,numdigitsafterdecimal [,includeleadingdigit [,useparensfornegativenumbers [,groupdigits]]]])
expression is the expression that you want formatted.
numdigitsafterdecimal (Optional) is a numeric value that indicates how many places to the right of the decimal separator should be displayed. If you omit this argument, the default value (-1) will be assumed and the settings from Control Panel will be used.
includeleadingdigit (Optional) indicates if a leading zero is displayed for fractional values. Use one of the following constants:
vbfalse 0 useparensfornegativenumbers (Optional) indicates if negative numbers are enclosed in parentheses. Use one of the following constants:
vbFalse 0
groupdigits (Optional) indicates if numbers are grouped using the thousand separator specified in Control Panel. Use one of the following constants:
vbfalse 0
|
|
Note
|
The way the currency symbol is
placed in relation to the currency value
|
|
Example |
MsgBox FormatCurrency(7500)
|
|
See Also |
FormatDateTime, FormatNumber and FormatPercent |
|
FormatDateTime |
Returns a string formatted as a date and/or time.
|
|
Syntax
|
FormatDateTime(date, [namedformat])
date is any valid date expression.
namedformat (Optional) is a numeric value that indicates the date/time format used. Use one of the following constants: |
|
Note
|
|
|
Example |
MsgBox FormatDateTime(Now, vbShortDate)
|
|
See Also |
FormatCurrency, FormatNumber, and FormatPercent |
|
FormatNumber |
Returns a string formatted as a number.
|
|
Syntax
|
FormatNumber (expression, [, numdigitsafterdecimal [, includeleadingdigit [, useparensfornegativenumbers [, groupDigits]]]])
expression is the expression that you want formatted.
numdigitsafterdecimal
(Optional) is a numeric value that indicates
|
|
|
includeleadingdigit (Optional) indicates if a leading zero is displayed for fractional values. Use one of the following constants:
vbfalse 0 useparensfornegativenumbers (Optional) indicates if negative numbers are enclosed in parentheses. Use one of the following constants:
vbfalse 0
groupdigits (Optional) indicates if numbers are grouped using the thousand separator specified in Control Panel. Use one of the following constants:
|
|
Note
|
|
|
Example |
MsgBox
FormatNumber("50000", 2, vbtrue, vbfalse, vbtrue)
|
|
See Also |
FormatCurrency, FormatDateTime, and FormatPercent |
|
FormatPercent |
Returns a string formatted as a percentage, like 50%.
|
|
Syntax
|
FormatPercent(expression, [, numdigitsafterdecimal [, includeleadingdigit [, useparensfornegativenumbers [,groupDigits]]]])
expression is any valid expression that you want formatted.
numdigitsafterdecimal
(Optional) is a numeric value that indicates
|
|
|
includeleadingdigit (Optional) indicates if a leading zero is displayed for fractional values. Use one of the following constants:
vbfalse 0
useparensfornegativenumbers (Optional) indicates if negative numbers are enclosed in parentheses. Use one of the following constants:
vbfalse 0
groupdigits (Optional) indicates if numbers are grouped using the thousand separator specified in Control Panel. Use one of the following constants:
|
|
Note
|
|
|
Example |
MsgBox FormatPercent(4 /
45)
|
|
See Also |
FormatCurrency, FormatDateTime, and FormatNumber |
|
InStr |
Returns an integer indicating the position for the first occurrence of a sub string within a string.
|
|
Syntax
|
InStr([start,] string1, string2[, compare])
start (Optional) is any valid non-negative expression indicating the starting position for the search within string1. Non-integer values are rounded. This argument is required if the compare argument is specified.
string1 is the string you want to search within.
string2 is the sub string you want to search for.
compare (Optional) indicates the comparison method used when evaluating. Use one of the following constants:
|
|
Syntax
|
vbBinaryCompare 0
(Default) Performs a binary comparison, i.e. a case
|
|
Note
|
|
|
Example |
Dim lngStartPos lngStartPos = 1 strSearchWithin = "This is a test string" strSearchFor = "t" ' Find the first occurrence lngFoundPos = InStr( lngStartPos, strSearchWithin, strSearchFor) ' Loop through the string Do While lngFoundPos > 0 ' Display the found position MsgBox lngFoundPos ' Set the new start
pos to lngStartPos = lngFoundPos + 1 ' Find the next occurrence lngFoundPos = InStr(
lngStartPos, strSearchWithin, strSearchFor)
The above code finds all occurrences of the letter t in string1, at position 11, 14 and 17. Please note that we use binary comparison here, which means that the uppercase T will not be "found". If you want to perform a case-insensitive search, you will need to specify the compare argument as vbTextCompare.
|
|
See Also |
InStrB, InStrRev |
|
InStrB |
Returns an integer indicating the byte position for the first occurrence of a sub string within a string containing byte data. |
|
Syntax
|
InStrB([start,] string1, string2[, compare])
start (Optional) is any valid non-negative expression indicating the starting position for the search within string1. Non-integer values are rounded. This argument is required, if the compare argument is specified.
string1 is the string containing byte data you want to search within.
string2 is the sub string you want to search for.
compare (Optional)
indicates the comparison method used when evaluating.
|
|
Note
|
|
|
Example |
Dim
lngStartPos
lngStartPos = 1 strSearchWithin = "This is a test string" strSearchFor = ChrB(0) lngFoundPos = InStrB( lngStartPos, strSearchWithin, strSearchFor) '
Loop through the string '
Set the new start pos to lngStartPos = lngFoundPos + 1 ' Find the next occurrence
lngFoundPos = InStrB( lngStartPos, strSearchWithin, strSearchFor)
The above code finds all occurrences of the byte value 0 in string1, at position 2, 4, 6, ...40 and 42. This is because only the first byte of the Unicode character is used for the character. If you use a double-byte character set like the Japanese, the second byte will also contain a non-zero value. |
|
See Also |
InStr, InStrRev |
|
InStrRev |
Returns an integer indicating the position of the
first occurrence of a sub string within a string starting from the end of the
string. This is the reverse functionality |
|
Syntax
|
InStrRev(string1, string2[, start[, compare]])
string1 is the string you want to search within.
string2 is the sub string you want to search for.
start (Optional) is any valid non-negative expression indicating the starting position for the search within string1; –1 is the default and it will be used if this argument is omitted.
compare (Optional) indicates the comparison method used when evaluating. Use one of the following constants:
|
|
Note
|
|
|
Example |
Dim
lngStartPos '
Loop through the string '
Set the new start pos to ' Find the next occurrence |
|
|
|
|
See Also |
InStr, InStrB |
|
Join |
Joins a number of substrings in an array to form the returned string. |
|
Syntax
|
Join(list[, delimiter])
list is a one dimensional
array that contains all the substrings that you want to join. delimiter (Optional) is the character(s) used to separate the substrings. A space character " " is used as the delimiter if this argument is omitted. |
|
Note
|
|
|
Example |
Dim strLights Dim arrstrColors(3)
arrstrColors(0) = "Red" strLights = Join( arrstrColors, ",")
strLights contains "Red,Yellow,Green". |
|
See Also |
Split |
|
LCase |
Converts all alpha characters in a string to lowercase. |
|
Syntax
|
LCase(string)
string is the string you want converted to lowercase. |
|
Note
|
|
|
Example |
MsgBox LCase("ThisIsLowerCase") MsgBox displays thisislowercase |
|
See Also |
UCase |
|
Left |
Returns length number of leftmost characters from string. |
|
Syntax
|
Left(string, length)
string is the string you want to extract a
number of characters from. |
|
Note
|
|
|
Example |
Dim strExtract
MsgBox displays Left.
|
|
See Also |
|
Len |
Returns the number of characters in a string.
|
|
Syntax
|
Len(string)
string is any valid string expression you want the length of.
|
|
Note
|
|
|
Example |
Dim strLength
MsgBox displays 17.
|
|
See Also |
|
LenB |
Returns the number of bytes used to represent a string.
|
|
Syntax
|
LenB(string)
string is any valid string expression you want the number of bytes for.
|
|
Note
|
|
|
Example |
Dim strLength
MsgBox displays 18.
|
|
See Also |
|
LTrim |
Trims a string of leading spaces; " " or Chr(32).
|
|
Syntax
|
LTrim(string)
string is any valid
string expression you want to trim leading
|
|
Note
|
|
|
Example |
Dim strSpaces
MsgBox displays Hello again *
|
|
See Also |
Left, Mid, Right, RTrim and Trim |
|
Mid |
Returns a specified number of
characters from any position in a
|
|
Syntax
|
Mid(string, start[, length])
string is any valid string expression you want to extract characters from.
start is the starting
position for extracting the characters. A zero-
length (Optional) is the number of characters you want to extract. All characters from start to the end of the string are returned if this argument is omitted or if length is greater than the number of characters counting from start.
|
|
Note |
|
|
Example |
Dim strExtract strExtract =
"Find ME in here"
MsgBox displays ME
|
|
See Also |
Left, Len, LenB, LTrim, MidB, Right, RTrim and Trim |
|
MidB |
Returns a specified number of bytes from any position in a string containing byte data. |
|
Syntax
|
MidB(string, start[, length])
string is a string expression containing byte data you want to extract characters from.
start is the starting
position for extracting the bytes. A zero-length string
length (Optional) is the number of bytes you want to extract. All bytes from start to the end of the string are returned if this argument is omitted or if length is greater than the number of bytes counting from start.
|
|
Note |
|
|
Example |
Dim strExtract
MsgBox displays ME , because VBScript uses 2 bytes to represent a character. The first byte contains the ANSI character code when dealing with 'normal' ANSI characters like M, and the next byte is 0. So byte 11 in the string is the first byte for the letter M and then we extract 4 bytes/2 characters.
|
|
See Also |
Left, Len, LTrim, Mid, Right, RTrim and Trim |
|
Replace |
Replaces a substring within a string with another substring a specified number of times.
|
|
Syntax
|
Replace(expression, find, replacewith[, start[, count[, compare]]]))
expression is a string expression that contains the substring you want to replace.
find is the substring you want to replace.
replacewith is the substring you want to replace with.
start (Optional) is the
starting position within expression for replacing the substring. 1
(default), the first position, will be used if this argument is omitted. You
must also specify the count argument if you want to use
|
|
|
count (Optional) is the number of times you want to replace find. -1 (default) will be used if this argument is omitted, which means all find in the expression. You must also specify the start argument if you want to use count.
compare (Optional) indicates the comparison method used when evaluating. Use one of the following constants:
|
|
Note
|
|
|
Example |
Dim strReplace strReplace =
Replace( "****I use text", "i", "You", , , _
|
|
See Also |
Left, Len, LTrim, Mid, Right, RTrim and Trim |
|
Right |
Returns length number of rightmost characters from string
|
|
Syntax
|
Right(string, length)
string is the string you
want to extract a number of characters from.
|
|
Note
|

RSS