# VBScript String Functions – An Excellent Guide for VBScript Tutorial 5

## VBScript Tutorial – Table of Content

VBScript Tutorial #2: VBScript Conditional Statements and Loops

VBScript Tutorial #3: VBScript Procedures

VBScript Tutorial #4: VBScript Error Handling and Execute VBScript

VBScript Tutorial #5: VBScript String Functions

VBScript Tutorial #7: VBScript Time Functions

VBScript Tutorial #8: VBScript Array Functions

In this VBScript Tutorial, we are going to learn about the most important and frequently used VBScript String Functions, including vbscript InStr, vbscript StrComp, vbscript Mid function, etc. All the vbscript string functions are explained with an example.

# VBScript Tutorial #5: VBScript String Functions

## VBScript String Functions:

While working with string in vbscript, we can use vbscript string functions to perform important string operations such as search, replace, extract, get the length, comparisons, etc. Through the “VBScript String Functions” article, we will explain the frequently used built-in VBScript string functions with examples.

### Important VBScript String Functions – Summary:

• vbscript SubString – This method is used to extract characters from string based on provided criteria.
• vbscript InStr – Find the position of a particular expression(first occurrence) within a string.
• vbscript Replace – Replace some part with another string.
• vbscript Mid – This method is used to extract characters from string based on provided criteria.
• vbscript Concatenation – This method is used to merge two or more string expressions.
• vbscript Left – Extract characters from the left side.
• vbscript StrComp – Compare two strings.
• vbscript Trim – Remove spaces from both the sides (start and end) of a string.
• vbscript Ltrim – This method clears the spaces from the left side from a specific string.
• vbscript Rtrim – This method clears the spaces from the right side from a specific string.
• vbscript UCase – Covert characters to upper case.
• vbscript LCase – Covert characters to lower case.
• vbscript Length – This method is use to find and the return the length for a specific string expression.
• vbscript Right – Extract characters from the right side.
• vbscript StrReverse – Reversing of a string.

# Important VBScript String Functions – Explanations:

All the important vbscript string functions are explained in this section with real live example.

## vbscript InStr:

The vbscript instr function finds the position of first occurrence of a particular expression available within a string and returns the position value.

#### Parameter Description:

Start – This parameter defines the start position of string1 from where searching or checking the first occurrence of string2 will be started. This is an optional parameter. By default, if nothing is specified, the vbscript starts with 1st position.

String 1 – This string is to be searched for the occurrence checking of another string.

String 2 – This is the string expression to search for.

Compare – This is an optional field used to define the comparison type between binary or textual. The default value is 0. The possible values are –

• 0 = vbBinaryCompare – Perform a binary checking
• 1 = vbTextCompare – Perform a textual checking

#### Example:

In this example of the vbscript InStr function, we are going to find and print the first occurrence of a search string.

string1 = "aabbccddee"
string2 = "bb"
nPostionOfOccurance = INSTR(1,string1,string2,1)
msgbox "Position of first occurance - " & nPostionOfOccurance


## vbscript string Replace:

The vbscript string replaces function is used to replace the specified parts of a string with another string for a predefined number of occurrences.

#### Parameter Description:

mainString – This is the main string that is to be updated for the replacement.

findString – This string portion will be replaced in the main string.

replaceWith – This is the replacement string.

StartPos – This parameter defines the start position of the main string from where searching will be started. This is an optional parameter. By default, if nothing is specified, the vbscript starts with 1st position. Before the start position, all the characters will be removed.

Count – This is an optional parameter that is used to define the numbers of substitutions to be done. The default value for the count parameter is -1, which defines that there is no limitation on number of substitutions to be done.

Compare – This is an optional field used to define the comparison type between binary or textual. The default value is 0. The possible values are –

• 0 = vbBinaryCompare – Perform a binary checking
• 1 = vbTextCompare – Perform a textual checking

#### Example:

In this example of the vbscript Replace function, we are going to replace all the occurrences of a particular string with another string.

mainString  = "aa bb cc dd bb ee"
findString  = "bb"
replaceWith = "zz"
startPos = 1
updatedString = Replace(mainString,findString,replaceWith,startPos)
msgbox "String after the replacement - " & updatedString


## vbscript Mid:

The vbscript Mid function returns the specified number of characters from a string.

#### Parameter Description:

string – The specified number of characters will be extracted from this string.

startPos – It defines the start position of the characters which is going to be extracted.

length – This is an optional field that defines the length of the extracted text. If the parameter is not provided, the vbscript mid function extract the entire string after the start position.

#### Example:

In this example of the vbscript Mid function, we are going to extract characters of length three from position 4.

source_string  = "aaabbbcccddd"
startPos = 4
length = 3
captured_string = Mid(source_string,startPos,length)
msgbox "Extracted string of length 3 from position 4 is  - " & captured_string


## vbscript substring:

There is no specific method with the name substring. But just like the java substring method, we can use the vbscript Mid function.

## vbscript string concatenation:

The vbscript string concatenation operator is used to add/ concrete two or more strings. The vbscript string concatenation operator is ‘&.’

#### Example:

In this example, we will add two strings using the vbscript string concatenation operator,

string1 = “abc” & “def”

After the execution, the variable string1 is going to hold the value as “abcdef”

## vbscript Left function:

The vbscript Left function extracts a specified number of characters from the left side of a string.

#### Parameter Description:

string – The specified number of characters will be extracted from this string from the left side.

length – It denotes the length of the characters which will be extracted from left side.

#### Example:

In this example of the vbscript Left function, we are going to extract characters of length three from the left side.

source_string  = "aaabbbcccddd"
length = 3
captured_string = Left(source_string,length)
msgbox "Extracted charecters from Left side  - " & captured_string

The vbscript Right function extracts a specified number of characters from the right side of a string.

#### Parameter Description:

string – The specified number of characters will be extracted from this string from the right side.

length – It denotes the length of the characters which will be extracted from right side.

#### Example:

In this example of the vbscript Right function, we are going to extract characters of length three from the right side.

source_string  = "aaabbbcccddd"
length = 3
captured_string = Right(source_string,length)
msgbox "Extracted charecters from Right side  - " & captured_string


## vbscript StrComp function:

The vbscript StrComp function is used to compare two strings and returns the result of the comparison.

#### Parameter Description:

string1 – One of the string expression parameter which required for the comparison.

String2 – Another string expression parameter required for the comparison.

Compare – This is an optional field used to define the comparison type between binary or textual. The default value is 0. The possible values are –

• 0 = vbBinaryCompare – Perform a binary checking
• 1 = vbTextCompare – Perform a textual checking

The vbscript StrComp function can return one of the following values:

• -1 (if string1 < string2)
• 0 (if string1 = string2)
• 1 (if string1 > string2)
• Null (if string1 or string2 is Null)

#### Example:

In this example of the vbscript StrComp function, we are going to see the results for three different comparison conditions.

'Condition when string1<string2
string1 = "abcd"
string2 = "wxyz"
result1 = StrComp(string1,string2,vbTextCompare )

'Condition when string1 = string2
string1 = "abcd"
string2 = "abcd"
result2 = StrComp(string1,string2,vbTextCompare )

'Condition when string1>string2
string1 = "wxyz"
string2 = "abcd"
result3 = StrComp(string1,string2,vbTextCompare )
msgbox "Result 1: " & result1 & ", Result 2: " & result2 & " and Result 3: " & result3


## vbscript Trim function:

The vbscript Trim function is used to clear all the spaces from both the side, i.e., from the beginning and end of the string.

#### Parameter Description:

string – It’s a string containing spaces at the left and right sides.

#### Example:

In this example of the vbscript Trim function, we are going to remove the spaces from both the sides of a string.

string1 = ” aaa bbb ccc ddd “

string2 = Trim(string1)

After the execution, the string2 variable will contain the value as “aaa bbb ccc ddd,” without the spaces on the left and right sides.

## vbscript Ltrim function:

The vbscript LTrim function is used to remove any spaces from the left side of the string.

#### Parameter Description:

string – It’s a string containing spaces on the left side.

#### Example:

In this example of the vbscript LTrim function, we are going to remove the spaces from the left side of a string.

string1 = ” aaa bbb ccc ddd “

string2 = Ltrim(string1)

After the execution, the string2 variable will contain the value as “aaa bbb ccc ddd,” without the spaces from the left side.

## vbscript Rtrim function:

The vbscript RTrim function is used to remove any spaces from the right side of the string.

#### Parameter Description:

string – It’s a string containing spaces on the right side.

#### Example:

In this example of the vbscript RTrim function, we are going to remove the spaces from the right side of a string.

string1 = ” aaa bbb ccc ddd “

string2 = Rtrim(string1)

After the execution, the string2 variable will contain the value as “ aaa bbb ccc ddd,” without the spaces from the right side.

## vbscript Uppercase i.e. vbscript UCase function:

The actual function name for vbscript Uppercase is vbscript Ucase function. The vbscript UCase function is used to convert the characters of any string(irrespective of case) into upper case characters.

#### Parameter Description:

string – It’s a string to convert into uppercase characters.

#### Example:

In this example of the vbscript UCase function, we are going to convert a string containing lower and upper cases into upper case characters.

string1 = “aBcD aabb”

string2 = Trim(string1)

After the execution, the string2 variable will contain the value as “ABCD AABB.”

## vbscript Lowercase i.e. vbscript LCase:

The vbscript LCase function is used to convert the characters of any string(irrespective of case) into lower case characters.

#### Parameter Description:

string – It’s a string to convert into lowercase characters.

#### Example:

In this example of the vbscript LCase function, we are going to convert a string containing lower and upper cases into lower case characters.

string1 = “aBcD aabb”

string2 = Trim(string1)

After the execution, the string2 variable will contain the value as “abcd aabb.”

## vbscript length function:

The vbscript Length function is used to find the length of a particular string. It returns the length as an integer value.

#### Parameter Description:

string – Any string expression.

#### Example:

In this example of the vbscript length function, we are going to find the length of any particular string expression.

string = “aBcD aabb”

strLength = Length(string)

After the execution strLength variable will contain the length of the string as 9.

## vbscript StrReverse function:

The vbscript StrReverse function is used to reversing any string.

#### Parameter Description:

string – Any string expression.

#### Example:

In this example of the vbscript StrReverse function, we are going to reversing the characters of a particular string.

string1 = “abcde”

string2 = Length(string1)

After the execution, the string2 variable will contain the reverse string as “edcba.”

## Conclusion:

Through this VBScript String Functions article, we have learned about the important VBScript String Functions, including vbscript InStr, vbscript StrComp, vbscript Mid funtions, etc. In the next vbscript tutorial, we will explain about VBScript Date and Time functions. Please click here to get more details.

About K Mondal

Hi, I am K. Mondal, I am associated with a leading organization. I am having 12+ years of working experience across domains e.g., application development, automation testing, IT Consultant. I am very much interested in learning different technologies. I am here to fulfill my aspiration and currently contributing as an Author and Website Developer both in LambdaGeeks.
Connect to LinkedIn- https://www.linkedin.com/in/kumaresh-mondal/