UFT Tutorial 27

UFT Class 27 (VBScript Built in Functions)
-----------------------------
1) Abs Function
(It returns absolute value)
Dim val
val = 100.75
Msgbox Abs(val) '100.75

val = 100.25
Msgbox Abs(val) '100.25

Msgbox Abs(100.60) '100.6

val = -100.85
Msgbox Abs(val) '100.85
------------------------------
2) Round Function

Dim val
val = 100.75
Msgbox Round(val) '101

val = 100.25
Msgbox Round(val) '100

Msgbox Round(100.60) '101

val = -100.85
Msgbox Round(val) '-101
----------------------------------
3) Asc Function

Dim val
val ="ABC"
Msgbox Asc(val)'65
'A to Z (65 to 90)
'a to z (97 to 122)
'0 to 9 (48 to 57)
Msgbox Asc("A") '65
Msgbox Asc("Z") '90
Msgbox Asc("a") '97
Msgbox Asc("z") '122
Msgbox Asc(1) '49
Msgbox Asc("*") '42
---------------------------
4) Chr Function
Dim val
val =65
Msgbox Chr(val)'A
'A to Z (65 to 90)
'a to z (97 to 122)
'0 to 9 (48 to 57)
Msgbox Chr(65) 'A
Msgbox Chr(90) 'Z
Msgbox Chr(97) 'a
Msgbox Chr(122) 'z
Msgbox Chr(49) '1
Msgbox Chr(42) '*
-----------------------------
5) Array FunctionDim a
Msgbox IsArray(a) 'False
a = Array(1, "VB", "Script", 1.34, #10/10/2010#)
Msgbox IsArray(a) 'True

Msgbox a(1) 'VB
Msgbox UBound (a) '4
------------------------------
6) IsArray Function
Dim a, b(4), c(), d(4, 5)
Msgbox IsArray(a) 'False

Msgbox IsArray(b) 'True

Msgbox IsArray(c) 'True

Msgbox IsArray(d) 'True
--------------------------------
7) IsNumeric
Msgbox IsNumeric(100) 'True
Msgbox IsNumeric("100") 'True
Msgbox IsNumeric(1.345) 'True
Msgbox IsNumeric("abc") 'False
Msgbox IsNumeric(#10/10/2010#) 'False

8) IsDate Function------------------------------
Msgbox IsDate(100) 'False
Msgbox IsDate(1.345) 'False
Msgbox IsDate("abc") 'False
Msgbox IsDate(#10/10/2010#) 'True
Msgbox IsDate(#10/10/10#) 'True
Msgbox IsDate(#20/10/2010#) 'True
'Msgbox IsDate(#20/13/2010#) 'Syntax Error
--------------------------------
9) IsEmpty Function
Dim val
Msgbox IsEmpty(val) 'True
val = 0
Msgbox IsEmpty(val)'False
-----------------------------------
10) Date Function

11) Time Function

12) Now Function

Example:

Dim a
a = Date
Msgbox a

Msgbox Date

Msgbox Time

Msgbox Now

Msgbox Date&" "& Time

Msgbox Time &" "& Date
--------------------------------
13) DateDiff Function

It 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/2014#

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

Dim val
val = "London"
Msgbox Len(val) '6
Msgbox Len("abc") '3
Msgbox Len(1234) '4
Msgbox Len(123.456)'7
Msgbox Len(#10/10/2010#) '10
Msgbox Len(#10/10/10#) '10
Msgbox Len(#December/10/2010#)'10
Msgbox Len(#September/10/2010#) '9
---------------------------------------
15) Left Function

Dim val
val ="Hyderabad"
Msgbox Left(val, 3) 'Hyd

Msgbox Left("Hyderabad", 3) 'Hyd
Msgbox Left(100, 1) '1
Msgbox Left(100.234, 4) '100.
Msgbox Left(#10-10-2010#, 5) '10/10
------------------------------------
16) Right Function

Dim val
val ="Hyderabad"
Msgbox Right(val, 3) 'bad

Msgbox Right("Hyderabad", 3) 'bad
Msgbox Right(100, 1) '0
Msgbox Right(100.234, 4) '.234
Msgbox Right(#10-10-2010#, 5) '/2010
---------------------------------------
17) Mid Function

Msgbox Left("Hyderabad", 3) 'Hyd
Msgbox Mid("Hyderabad", 1, 3)'Hyd

Msgbox Right("Hyderabad", 3) 'bad
Msgbox Mid("Hyderabad", 7) 'bad

Msgbox Mid("Hyderabad", 4, 3) 'era
-------------------------------------
18) StrComp Function

It compares two strings based on compare mode

Compare Modes:

i) Binary Comparison 0

ii) Textual Comparison 1
---------------------------
Result Criteria:

Comparisons in Computer Programming

i) 2-way comparison (True/False)

ii) 3-way Comparison (=, >, <)
---------------------------------------
i) If string1 = string2 then result is 0

ii) If string1 > string2 then result is 1

iii) If string1 < string2 then result is -1
------------------------------------------
Note: Default compare mode for StrComp Function is Binary mode.

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, 0) '1
Msgbox StrComp(str1, str2) '1
Msgbox StrComp(str1, str2, 1) '0
-----------------------------------------
19) LCase Function

Msgbox LCase("LONDON") 'london
Msgbox LCase("LONdon") 'london
Msgbox LCase("london") 'london
Msgbox LCase("LONDON123") 'london123
Msgbox LCase(123) '123
--------------------------------
20) UCase Function
Msgbox UCase("LONDON") 'LONDON
Msgbox UCase("LONdon") 'LONDON
Msgbox UCase("london") 'LONDON
Msgbox UCase("LONDON123") 'LONDON123
Msgbox UCase(123) '123
----------------------------------------
21) Trim Function

22) RTrim Function

23) LTrim Function

Dim val
val ="                VB Script                 "
Msgbox val
Msgbox Trim(val)
Msgbox RTrim(val)
Msgbox LTrim(val)
-------------------------------------
24) VarType Function

Dim val
Msgbox VarType(val) '0 for Empty

val= 123
Msgbox VarType(val) '2 for Integer

val="abc"
Msgbox VarType(val) '8 for string

val= "123"
Msgbox VarType(val) '8 for String

val= 1.23
Msgbox VarType(val) '5 for Double

val = #10/10/2010#
Msgbox VarType(val) '7 for Date

Set val = CreateObject("Scripting.FileSystemObject")
Msgbox VarType(val) '9 for Automation Object
------------------------------------------------
25) Cint Function

Dim val
val = InputBox("Enter a Value")

Msgbox VarType(val) '8 for String

val = Cint(val)
Msgbox VarType(val)
------------------------------
26) Cdbl Function
Dim val
val = InputBox("Enter a Value")

Msgbox VarType(val) '8 for String

val = Cdbl(val)
Msgbox VarType(val)
--------------------------------
27) Split Function

Dim val, a
Msgbox IsArray(a) 'False
val ="VB Script Language"
a = Split(val)
Msgbox IsArray(a) 'True

Msgbox a(1) 'Script

Msgbox UBound(a) '2
--------------------------
Dim val, a
Msgbox IsArray(a) 'False
val ="VB@Script@Language"
a = Split(val, "@")
Msgbox IsArray(a) 'True

Msgbox a(1) 'Script

Msgbox UBound(a) '2
-------------------
Dim val, a
Msgbox IsArray(a) 'False
val ="VB@$#Script@$#Language"
a = Split(val, "@$#")
Msgbox IsArray(a) 'True

Msgbox a(1) 'Script

Msgbox UBound(a) '2
---------------------------------

0 comments:

Post a Comment