VBScript Built in Functions
1) Asc Function
It returns ANSI character code for first letter of a string or number.
‘A to z (65 to 90)
‘a to z (97 to 122)
‘0 to 9 (48 to 57)
Example:
Dim a
a =”ABCD”
Msgbox Asc(a) ’65
Msgbox Asc(“A”) ’65
Msgbox Asc(“Z”) ’90
Msgbox Asc(“a”) ’97
Msgbox Asc(“z”) ‘122
Msgbox Asc(1) ’49
Msgbox Asc(“*”) ’42
2) Chr Function
Returns Character value for ANSI character code.
Example:
Msgbox Chr(65) ‘A
Msgbox Chr(90) ‘Z
Msgbox Chr(97) ‘a
Msgbox Chr(122) ‘z
Msgbox Chr(48) ‘0
Msgbox Chr(42) ‘*
3) Abs Function
It returns absolute value for numbers
Example:
Msgbox Abs(100.45)’100.45
Msgbox Abs(100.85)’100.85
Msgbox Abs(-100.85)’100.85
4) Round Function
It rounds the value to nearest integer.
Example:
Msgbox Round(100.45)’100
Msgbox Round(100.85)’101
Msgbox Round(-100.85)’-101
5) Array Function
It assigns series values at a time to a Variable.
Example:
Dim a
Msgbox IsArray(a) ‘False
a = Array(10, 20, 30, 40, 1.234, “abcd”, #10/10/2010#)
Msgbox IsArray(a) ‘True
Msgbox a(5) ‘abcd
Msgbox UBound(a) ‘6
6) IsArray Function
It checks weather the Variable is Array Variable or not? And returns Boolean result(True/False).
Example:
Dim a, b(3), c(), d(4, 5)
Msgbox IsArray(a) ‘False
Msgbox IsArray(b) ‘True
Msgbox IsArray(c) ‘True
Msgbox IsArray(d) ‘True
7) IsNumeric Function
It checks weather the value is Numeric value or not? And returns Boolean result (True/False).
Example:
Dim a
a =”London”
Msgbox IsNumeric(a) ‘False
Msgbox IsNumeric(“London”)’False
Msgbox IsNumeric(100) ‘True
Msgbox IsNumeric(1.234) ‘True
Msgbox IsNumeric(“123”) ‘True
Msgbox IsNumeric(#10/10/2010#) ‘False
Msgbox IsNumeric(“123*”) ‘False
8) IsDate Function
It checks weather the value is Date type data or not? And returns Boolean result (True/False).
Example:
Dim a
a =#10/10/2010#
Msgbox IsDate(a) ‘True
Msgbox IsDate(“London”)’False
Msgbox IsDate(100) ‘False
Msgbox IsDate(1.234) ‘False
Msgbox IsDate(“123”) ‘False
Msgbox IsDate(#10/10/10#) ‘True
Msgbox IsDate(#10-10-10#) ‘True
Msgbox IsDate(#Oct/10/10#) ‘True
Msgbox IsDate(#December/10/10#) ‘True
9) IsEmpty Function
It checks weather the Variable is Empty variable or not?
Example:
Dim a
Msgbox IsEmpty(a) ‘True
a =0
Msgbox IsEmpty(a) ‘False
10) Date Function
It returns Local System Current Date value.
11) Time Function
It returns Local System Current Time value.
12) Now Function
It returns Local System Date and Time value.
Example:
Dim a
a = Date
Msgbox a
Msgbox Date
Msgbox Time
Msgbox Now
msgbox Date &” “& Time
Msgbox Time&” “&Date
13) DateDiff Function
Returns Date difference between two dates based on interval.
It deducts first date from second date.
Example:
Dim date1, date2
date1 = #10/10/2010#
date2 = #10/10/2012#
Msgbox DateDiff(“d”, date1, date2) &” Days”
Msgbox DateDiff(“yyyy”, date1, date2) &” Years”
Msgbox DateDiff(“q”, date1, date2) &” Quarters”
Msgbox DateDiff(“m”, date1, date2) &” Months”
Msgbox DateDiff(“w”, date1, date2) &” Weeks”
Msgbox DateDiff(“h”, date1, date2) &” Hours”
Msgbox DateDiff(“n”, date1, date2) &” Minutes”
Msgbox DateDiff(“s”, date1, date2) &” Seconds”
14) Len Function
It returns length of a string or number.
Example:
Dim a
a=100
Msgbox Len(a) ‘3
Msgbox Len(100) ‘3
Msgbox Len(1.234)’5
Msgbox Len(“India”)’5
Msgbox Len(“@#$”) ‘3
Msgbox Len(#10/10/2010#) ’10
Msgbox Len(#10/10/10#) ’10
Msgbox Len(#Sep/10/2010#) ‘9
Msgbox Len(#December/10/2010#) ’10
Count length for “abc
15) Left Function
It returns a specified number of characters from left side of a String or number.
Example:
Dim a
a =”Hyderabad”
Msgbox Left(a, 3) ‘Hyd
Msgbox Left(“Hyderabad”, 3)’Hyd
Msgbox Left(100, 1) ‘1
Msgbox Left(9849012345, 5) ‘98490
Msgbox Left(12.345, 3) ’12.
Msgbox Left(#10-10-2010#, 5)’10/10
16) Right Function
It returns a specified number of characters from right side of a String or number.
Example:
Dim a
a =”Hyderabad”
Msgbox Right(a, 3) ‘bad
Msgbox Right(“Hyderabad”, 3)’bad
Msgbox Right(100, 1) ‘0
Msgbox Right(9849012345, 5) ‘12345
Msgbox Right(12.345, 3) ‘345
Msgbox Right(#10-10-2010#, 5)’/2010
17) Mid Function
It returns a specified number of characters from a string or number.
Example:
Msgbox Mid(“Hyderabad”, 4, 3)’era
Msgbox Mid(“Hyderabad”, 4)’erabad
Msgbox Left(“Hyderabad”, 3) ‘Hyd
Msgbox Mid(“Hyderabad”, 1, 3) ‘Hyd
Msgbox Right (“Hyderabad”, 3) ‘bad
Msgbox Mid(“hyderabad”, 7) ‘bad
18) Trim Function
It removes spaces from both sides of a string or number
19) LTrim Function
It removes left side spaces from both sides of a string or number
20) RTrim Function
It removes right side spaces from both sides of a string or number
Example:
Dim a
a = ” VB Script ”
Msgbox Trim (a)
Msgbox LTrim (a)
Msgbox RTrim (a)
Dim a
a = “VB Script”
b = Split(a)
Msgbox b(0)&b(1) ‘VBScript
21) LCase Function
It converts values to lower cases
Example:
Dim a
a =”LONDON”
Msgbox LCase(a) ‘london
Msgbox LCase(“LONDON”)’ london
Msgbox LCase(“LONdon”) ‘london
Msgbox LCase(“london”) ‘london
Msgbox LCase(“LONdon123”) ‘london123
Msgbox LCase(123)’123
22) UCase Function
It converts values to upper cases
Example:
Dim a
a =”LONDON”
Msgbox UCase(a) ‘london
Msgbox UCase(“LONDON”)’ LONDON
Msgbox UCase(“LONdon”) ‘LONDON
Msgbox UCase(“london”) ‘LONDON
Msgbox UCase(“LONdon123”) ‘LONDON123
Msgbox UCase(123)’123
23) Cint Function
It converts String sub type to integer sub type.
It doesn’t convert Alfa bytes.
Example:
Dim a
a =”123″
MSgbox VarType(a) ‘8
a = Cint(a)
MSgbox VarType(a) ‘2
a =”abc”
MSgbox VarType(a)’8
a = Cint(a)
MSgbox VarType(a) ‘Error
24) Cdbl Function
It converts string type data into Double sub type.
Example:
Dim a
a =”123.234″
MSgbox VarType(a) ‘8
a = Cdbl(a)
MSgbox VarType(a) ‘5
a =”abc”
MSgbox VarType(a)’8
a = Cdbl(a)
MSgbox VarType(a) ‘Error
25) Split Function
Splits a String based on delimiter, default delimiter is space.
Example:
Dim a, b
a = “VB Script Language”
b = Split(a)
Msgbox b(0)’VB
Msgbox b(1)’Script
Msgbox b(2)’language
Dim a, b
a = “VB,Script,Language”
b = Split(a, “,”)
Msgbox b(0)’VB
Msgbox b(1)’Script
Msgbox b(2)’language
Dim a, b
a = “VB$#%Script$#%Language”
b = Split(a, “$#%”)
Msgbox b(0)’VB
Msgbox b(1)’Script
Msgbox b(2)’language
Dim email
email = “abcdefg@gmail.com”
a =Split(email,”@”)
b= Split(a(1), “.”)
Msgbox a(0)’abcdefg
Msgbox b(0)’gmail
Msgbox b(1)’com
26) Join Function
It joins all elements of an Array.
Example:
Dim a(2)
a(0) = “VB”
a(1) = “Script”
a(2) =”Language”
Msgbox Join(a, “,”)’VB,Script,Language
Dim a(2)
a(0) = “VB”
a(1) = “Script”
a(2) =”Language”
Msgbox Join(a)’VB Script Language
27) LBound Function
28) UBound Function
Example:
Dim a
a = Array(1, 3, 4, 5, 7, 8)
Msgbox LBound(a) ‘0
Msgbox UBound(a) ‘5
29) InputBox Function
30) Msgbox Function
Example:
Dim a
a = InputBox(“Enter a Value”)
msgbox a
————–
Dim a, b
a = 100
b = 200
Msgbox a
Msgbox “Value of a is: “& a
Msgbox a & ” Hundred”
Msgbox “Value of a is: “& a &” Value of b is: “&b
32) VarType Function
It checks variable data sub type and returns constant based Result.
Example:
Dim a, b
a= 100
Msgbox VarType(a) ‘2 for Integer
Msgbox VarType(100) ‘2 for Integer
Msgbox VarType(b) ‘0 for Uninitialized /Empty
b=”India”
Msgbox VarType(b) ‘8 for String
b=”123″
Msgbox VarType(b) ‘8 for String
b=1.23
Msgbox VarType(b) ‘5 for Double
b=#10/10/2015#
Msgbox VarType(b) ‘7 for Date
Set b = CreateObject(“Scripting.FileSystemObject”)
Msgbox VarType(b) ‘9 for object
33) CreateObject Function
It creates Automation object in a specified class
Syntax:
Set Variable = CreateObject(“Class Value”)
Example:
Dim objFso
Msgbox varType (objFso) ‘0
‘Storing Object Reference
Set objFso = CreateObject(“Scripting.FileSystemObject”)
Msgbox varType (objFso) ‘9
Dim objExcel
Set objExcel = CreateObject(“Excel.Application”)
Dim objWord
Set objWord = CreateObject(“Word.Application”)
Dim objConnection
Set objConnection = CreateObject(“Adodb.Connection”)
Dim objDictionary
Set objDictionary = CreateObject(“Scripting.Dictionary”)
34) StrComp Function
It compares two strings based on Compare mode and returns constant based Result.
Compare modes
0 for Binary
1 for Textual
Result Criteria
if str1 = str2 then 0
if str1 > str2 then 1
if str1 < str2 then -1
Example:
Dim str1, str2
str1 =”UFT”
str2=”uft”
Msgbox StrComp(str1, str2, 0) ‘-1
Msgbox StrComp(str1, str2) ‘-1
Msgbox StrComp(str1, str2, 1)’0
str1 =”uFT”
str2=”Uft”
Msgbox StrComp(str1, str2) ‘1
Msgbox StrComp(str1, str2, 1) ‘0