Updated April 10, 2023
VBScript String Functions
VBScript is a scripting language that was primarily developed by Microsoft and is modeled on Visual Basic. The windows system administrators have the privilege of generating powerful tools for handling and managing computers with subroutines, error handling and many other different programming constructs. The user, therefore, has huge control over some aspects of the computing environment.
VBScript also uses the component object model, which can be used to access the environmental elements. As an example, the FileSystemObject (FSO) can be used to create, update, read and delete files. This scripting language is by default installed in almost every desktop-based OS. It must be executed inside a host environment.
The language features contain Visual Basic language on which VBScript is modeled. Therefore this can be reviewed by making use of similar structures and categories such as control structures, procedures, constants, user interaction, variables, date and time functions, array handling, mathematical functions, error handling, regular expressions, objects, string manipulation, etc. This VBScript has, like other languages, many string functions which are used to play and work with strings, which is a sequence of characters consisting of either numbers or alphabets or any other special characters or every one of them.
A variable is said to be of string type when enclosed within double quotations. These kinds of functions are in themselves predefined functions that aim to work with developers by making use of strings in a very efficient way. The string is one among the different data types used in the visual basic language. Below are some functions discussed as a part of string functions.
Examples of VBScript String Functions
The string function is easy to use. Here we will discuss how to use string functions in VBScript with the help of examples.
1. InStr: This function is used to return the very first occurrence of any given substring. The searching within the string happens from left to right.
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var = "Microsoft VBScript"
document.write("Line 1 : " & InStr(1,var,"s") & "<br />")
document.write("Line 2 : " & InStr(7,var,"s") & "<br />")
</script>
</body>
</html>
Output:
Line 1: 6
Line 2: 0
2. InstrRev: As the name suggests, this function is a lot more like the InStr function, with the difference being that the searching happens from right to left. It is also used to find the first occurrence within the specified string
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var = "Microsoft VBScript"
document.write("Line 1 : " & InStrRev(var,"s",10) & "<br />")
document.write("Line 2 : " & InStrRev(var,"s",7) & "<br />")
</script>
</body>
</html>
Output:
Line 1: 6
Line 2: 6
3. LCase: This string function in VBScript is used to return the lower casing of the string specified.
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var = "VBSCRIPT"
document.write("Line 1 : " & LCase(var) & "<br />")
</script>
</body>
</html>
Output:
Line 1: vbscript
4. UCase: This function is used to return the upper case of the string which is specified.
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var = "VBScript"
document.write("Line 1 : " & UCase(var) & "<br />")
</script>
</body>
</html>
Output:
Line 1: VBSCRIPT
5. Left: This function is used to return a specified number of characters from the string’s left side.
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var = "Microsoft VBScript"
document.write("Line 1 : " & Left(var,2) & "<br />")
</script>
</body>
</html>
Output:
Line 1: Mi
6. Right: This function is used to return a specific number of characters from the string’s right side.
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var = "Microsoft VBScript"
document.write("Line 1 : " & Right(var,2) & "<br />")
</script>
</body>
</html>
Output:
Line 1: pt
7. Mid: This string function in VBScript is used to return a specified number of characters from a given string.
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var = "Microsoft VBScript"
document.write("Line 1 : " & Mid(var,2) & "<br />")
</script>
</body>
</html>
Output:
Line 1: icrosoft VBScript
8. LTrim: This function is used to return the string text after removing all the blank white spaces from the left-hand side of it.
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var = " Microsoft VBScript"
document.write("After Ltrim : " & LTrim(var) & "<br />")
</script>
</body>
</html>
Output:
After Ltrim: Microsoft VBScript
9. Rtrim: This function is used to trim all the right-hand side white spaces of a given string and return a string with no white spaces.
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var = "Microsoft VBScript "
document.write("After Ltrim : " & LTrim(var) & "<br />")
</script>
</body>
</html>
Output:
After Ltrim: Microsoft VBScript
10. Len: This function is used to calculate the length of the given string.
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var1 = "Microsoft VBScript"
document.write("Length of var1 : " & Len(var1)
</script>
</body>
</html>
Output:
Length of var1: 18
11. Replace: This function is used to return a string when another string replaces one string.
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var = "This is VBScript Programming"
'VBScript to be replaced by MS VBScript
document.write("Line 1: " & Replace(var,"VBScript","MS VBScript") & "<br />")
</script>
</body>
</html>
Output:
Line 1: This is MS VBScript Programming
12. Space: This VBScript string function is used to fill the space in a specified string with a particular number of spaces.
For example,
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var1 = "Microsoft"
var2 = "VBScript"
document.write(var1 & Space(2)& var2)
</script>
</body>
</html>
Output:
Microsoft VBScript
Conclusion
There are many VBScript string functions, and in this article, we have discussed some of the majorly used ones. Go ahead, start playing with these functions and explore what new can be done.
Recommended Articles
We hope that this EDUCBA information on “vbscript string functions” was beneficial to you. You can view EDUCBA’s recommended articles for more information.