Menu Bar of the blog

Tuesday, 4 November 2014

Tutorial#3.2: Sub-routines and In-built Functions in VB Script

Sub-Routines are similar to functions but they cannot return any value. For Eg:-
sum 3,4
Sub sum(a, b)
    msgbox a+b
End Sub

There are a number of in-built functions in function libraries of VB. The complete list is given at - UFT Help -> VB Script -> VB Script Language Reference -> Functions. Over here, we are going to cover few of the important in-built functions:-
  1. Left
  2. Right
  3. Mid
  4. Split
  5. UCase/LCase
  6. ltrim, rtrim, trim
  7. cint, cstr
  8. isnumeric
  9. date
  10. time
  11. now
  12. datevalue
  13. datepart
  14. datediff
  15. hour
  16. min
  17. sec
Left - It is used to return left part of a string.
str = "Yes!! Today is Friday!!"
msgbox left(str, 5)

Output: Yes!!

Mid - It is used to return middle part of a string.
str = "Yes!! Today is Friday!!"
msgbox mid(str, 7,5)

Output: Today
Hence, syntax of mid is - mid(<string>, <start>, [optional] length)

Right - It is used to return Right part of a string.
str = "Yes!! Today is Friday!!"
msgbox Right(str, 8)

Output: Friday!!


Split - It is used to split an expression into various elements. And, it stores the elements of the expression in the form of an array. Eg:-
str = "Yes!! Today is Friday!!"arr = split(str, " ")     ' Here, space in the second parameter acts as a Delimiter.
For Iterator = 0 To ubound(arr)
    Print arr(iterator)  
Next

Output: Yes!!
Today
is
Friday!!


UCase and LCase - They are used to convert the case of a string to upper or lower.
Print ucase("abcdEFGH")
Print lcase("abcdEFGH")

Output: ABCDEFGH
abcdefgh

LTrim, RTrim and Trim - LTrim removes the blank spaces from left hand side, RTrim removes the blank spaces from right hand side and Trim removes the blank faces from both the sides.

IsNumeric, CStr and CInt - IsNumeric function tells us whether a string is numeric or not. And, CInt converts a string to an integer. Similarly, CStr is used to convert an Integer to a string.
= "44"
= "33"
If IsNumeric(a) and isnumeric(b) Then
    msgbox CInt(a) + CInt(b)  
End If

Output: 77.

a = 124
msgbox CStr(a)

Date, Time and Now - Date retruns Date, Time returns Time and Now returns both.
msgbox Date
msgbox Time
msgbox Now

msgbox datevalue("10 november, 2013") - This returns date in mm/dd/yyyy format.

How to find Date difference between 2 given dates?
For this, there is an in-built function called as dateDiff.
d1 = dateValue("november 10, 2010")
d2 = dateValue("november 10, 2011")
msgbox datediff("d", d1, d2) - Output: 365 - so, dateDiff returns difference in days.
msgbox datePart("d", d1) - Output: 10 - retruns date part of a date.
msgbox datePart("m", d1) - Output: 11 - returns month part of a date.


TIME - 
print time - Returns current time.
print hour(time) - Returns hour part of time.
print minute(time) - Returns minute part of time.
print second(time) - Returns second part of time.
Output:
11:53:27 PM
23
53
27
==============***********************===============

No comments:

Post a Comment