Skip to main content

VB Script - Part I

VB Script - Part I


VBScript is an interpreted script language from Microsoft that is a subset of its Visual Basic programming language designed for
interpretation by Web browsers.

String Functions
Len Function
Len : Returns the number of characters in a string or the number of bytes required to store a variable. 
 
Syntax: Len(string | varname)
 
Arguments:
 
string: Any valid string expression. If string contains Null, Null is returned.
varname:  Any valid variable name. If varname contains Null, Null is returned.

Example:
Str="Welcome to the World of QTP"

Print Len(Str)          
' Output --> 27

Print  Len("Good Morning")  
' Output--> 12

LCase Function
LCase: Returns a string that has been converted to lowercase.
 
Syntax: LCase(string)
 
Aruguments:
 
String: Any valid string expression. If string contains Null, Null is returned.
 
Example:
Str="Welcome to the World of QTP"

Print LCase(Str) 
'Output --> welcome to the world of qtp

Print  Lcase("Good Morning")   
'Output --> good morning

UCase Function
Ucase : Returns a string that has been converted to uppercase.
 
Syntax: UCase(string)
 
Arguments:
 
String: Any valid string expression. If string contains Null, Null is returned.
 
Example:
Str="Welcome to the World of QTP"

Print Ucase(Str) 
'Output --> WELCOME TO THE WORLD OF QTP

Print  Ucase("Good Morning")              
'Output --> GOOD MORNING

Left Function
Left: Returns a specified number of characters from the left side of a string.
 
Syntax: Left(string, length)
 
Arguments:
 
String:String expression from which the leftmost characters are returned. If string contains Null, Null is returned.
length:Numeric expression indicating how many characters to return. If 0, a zero-length string("") is returned. If greater than
or equal to the number of characters in string, the entire string is returned.
 
Example: 
Str="Welcome to the World of QTP"

print  Left(Str,3)                                                   
'Output --> Wel

Print Left("Good Morning",4)                              
'Output --> Good


Right Function


Right: Returns a specified number of characters from the right side of a string.
 
Syntax: Right(string, length)
 
Arguments:
 
String: String expression from which the rightmost characters are returned. If string contains Null, Null is returned.
length:Numeric expression indicating how many characters to return. If 0, a zero-length string is returned. If greater than or equal
 to the number of characters in string, the entire string is returned.
 
Example: 
Str="Welcome to the World of QTP"

print  Right(Str,12)                                                
'Output --> World of QTP

Print Right("Good Morning",7)                                            
'Output -->  Morning


Mid Function

Mid: Returns a specified number of characters from a string.
 
Syntax: Mid(string, start[, length])
 
Aruguments:
 
String:String expression from which characters are returned. If string contains Null, Null is returned.
Start:Character position in string at which the part to be taken begins. If start is greater than the number of characters in string, Mid returns a zero-length string ("").
length:Number of characters to return. If omitted or if there are fewer than length characters in the text (including the character
at start), all characters from the start position to the end of the string are returned.
 
Example:
Str="Welcome to the World of QTP"

print  Mid(Str,9,12)                                            
'Output --> to the World

Print Mid("Good Morning",6,7)                        
'Output --> Morning

Print Mid("Good Morning",6)                        
'Output --> Morning


Replace Function

Replace: Returns a string in which a specified substring has been replaced with another substring a specified number of times.
 
Syntax: Replace(expression, find, replacewith[, start[, count[, compare]]])
 
Arguments:
 
expression:(Required) String expression containing substring to replace.
find:(Required) Substring being searched for.
replacewith:(Required) Replacement substring.
start:(Optional) Position within expression where substring search is to begin. If omitted, 1 is assumed. Must be used in
 conjunction with count.
count:(Optional) Number of substring substitutions to perform. If omitted, the default value is -1, which means make all
 possible substitutions. Must be used in conjunction with start. 
 
compare:(Optional) Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values. If omitted, the default value is 0, which means perform a binary comparison. 
 

 
Example:
Str="Welcome to the World of QTP"

print Replace(Str, "to","into")                                                    
'Output -->     Welcome into the World of QTP

Print Replace("Good Morning","Morning","Night")                
'Output -->  Good Night

Print Replace("Quick Test ProfeSsional","s","x",5,3,0)            
'Output -->  k Text ProfeSxional

Print Replace("Quick Test ProfeSsional","s","x",5,3,1)                    
'Output -->    k Text Profexxional


Space Function


Space: Returns a string consisting of the specified number of spaces.
 
Syntax: Space(number)
 
Arguments:
 
number:number of spaces you want in the string.
 
Example: 
Str="Welcome to the World of QTP"

'Space

Str1="Welcome"
Str2="to the World"
Str3="of QTP"


Print  Str1 & Space(2) & Str2 & Space(2) &Str3                    
'Output-->Welcome  to the World  of QTP


Split Function


Split: Returns a zero-based, one-dimensional array containing a specified number of substrings.
 
Syntax: Split(expression[, delimiter[, count[, compare]]])
 
Arguments:
 
expression:(Required) String expression containing substrings and delimiters. If expression is a zero-length string, Split returns
 an empty array, that is, an array with no elements and no data.
delimiter:(Optional) String character used to identify substring limits. If omitted, the space character (" ") is assumed to be the
 delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.
count:(Optional) Number of substrings to be returned; -1 indicates that all substrings are returned.
compare:(Optional) Numeric value indicating the kind of comparison to use when evaluating substrings. 
 
Example:
'Space as Delimiter
Str="Welcome to the World of QTP"

'Split

SArray=Split(Str," ",-1)

For  i= 0 to UBound(SArray)

    Print  SArray(i)

Next

'Comma As Delimitter

Str="Welcome,to,the,World,of,QTP"

'Split

SArray=Split(Str,",",-1)

For  i= 0 to UBound(SArray)

    Print  SArray(i)

Next


StrComp Function

StrComp: Returns a value indicating the result of a string comparison. 
 
Syntax: StrComp(string1, string2[, compare])
 
Arguments:
 
string1:(Required) Any valid string expression.
string2:(Required) Any valid string expression.
compare: (Optional) Numeric value indicating the kind of comparison to use when
evaluating strings. If omitted, a binary comparison is  performed.
 
Example:
Str="Welcome to the World of QTP"

StrComp
Str1 = "QTP"
Str2 = "qtp"                                   

Print StrComp(Str1, Str2, 1)                           
'Output--> Returns 0 , which means Str1 is equal to Str2 for  textual comparison

Print StrComp(Str1, Str2, 0)                           
'Output--> Returns -1 , which means Str2 is  greater than  Str1 for  Binary comparison

Print StrComp(Str2, Str1)                               
'Output-->Returns 1, which means Str2 is  greater than  Str1 for  Binary comparison

Print StrComp(Str1, Str2)                                 
'Output-->Returns -1, which means Str1 is  less than  Str2  for  Binary comparison


StrReverse Function


StrReverse: Returns a string in which the character order of a specified string is reversed.
 
Syntax: StrReverse(string1)
 
Arguments:
 
string1:string whose characters are to be reversed. If string1 is a zero-length string (""),
a zero-length string is returned. If string1 is Null, an error occurs.

Example: 
Str="Welcome to the World of QTP"

StrReverse

print  StrReverse(Str)                                      
'Output-->PTQ fo dlroW eht ot emocleW

Print  StrReverse("Quick Test ProfeSsional")                 
'Output-->lanoisSeforP tseT kciuQ


LTrim; RTrim; and Trim Functions


LTrim; RTrim; and Trim: Returns a copy of a string without leading spaces (LTrim), trailing spaces (RTrim), or both leading
and trailing spaces (Trim).
 
Syntax:  LTrim(string)
      RTrim(string)
      Trim(string)
  
Arguments:
 
string:The string argument is any valid string expression. If string contains Null, Null is returned.
 
Example: 
'LTrim 

Print Ltrim("       Welcome to QTPWorld.com                 ") 
                           
 'Output-->"Welcome to QTPWorld.com 
Example: 
'RTrim  

Print Rtrim("           Welcome to QTPWorld.com                          ")                    
'Output--> "            Welcome to QTPWorld.com"
Example: 
'Trim

Print     trim("              Welcome to QTPWorld.com               ")                
'Output-->"Welcome to QTPWorld.com"

InStr Function
InStr: Returns the position of the first occurrence of one string within another.
 
Syntax: InStr([start, ]string1, string2[, compare])
 
Arguments:
 
start:(Optional) Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. If start contains Null, an error occurs. The start argument is required if compare is specified.
 
string1:(Required) String expression being searched.
string2:(Required) String expression searched for.
compare:(Optional) Numeric value indicating the kind of comparison to use when evaluating substrings.
 
 
Example: 
Str="How do you DO ?"

'InStr

Print Instr(1, Str, "DO", 1)                
'Output--> 5 , which means  it found the string in 5th position for  textual comparison

Print Instr(1, Str, "DO", 0)                
'Output--> 12 , which means  it found the string in 12th position for binary comparison

Print Instr(6, Str, "do", 0)                    
'Output--> 0 , which means  it  didnot found the string after the 6th position  
 for binary comparison


InStrRev Function


InStrRev: Returns the position of an occurrence of one string within another, from the end of string. 
 
Syntax: InStrRev(string1, string2[, start[, compare]])
 
Arguments:
 
string1:(Required) String expression being searched.
string2:(Required) String expression being searched for.
start: (Optional) Numeric expression that sets the starting position for each search. If omitted, -1 is used, which means that the search begins at the last character position. If start contains Null, an error occurs. 
 
compare:(Optional) Numeric value indicating the kind of comparison to use when evaluating substrings. If omitted, a binary comparison  is performed.  
 
 
Example: 
Str="How do you DO ?"

'InStrRev

Print InStrRev(Str,"DO",-1,1)                
'Output--> 12 , which means  it found the string in 12th position for  textual comparison

Print InStrRev(Str,"do",-1,0)            
'Output--> 5 , which means  it found the string in 5th position for binary comparison

Print InStrRev(Str,"DO",13,0)            
'Output--> 12 , which means  it found the string in  12th position for binary comparison

Print InStrRev(Str,"DO",10,1)            
'Output--> 5 , which means  it found the string in 5th position for  textual comparison 

Comments

Popular posts from this blog

SSO with SAML login scenario in JMeter

SAML(Security Assertion Markup Language) is increasingly being used to perform single sign-on(SSO) operations. As WikiPedia puts it, SAML is an XML-based open standard data format for exchanging authentication and authorization data between parties, in particular, between an identity provider and a service provider. With the rise in use of SAML in web applications, we may need to handle this in JMeter. This step-by-step tutorial shows SAML JMeter scenario to perform login operation. First request from JMeter is a GET request to fetch Login page. We need to fetch two values ‘SAMLRequest’ and ‘RelayState’ from the Login page response data. We can do this by using  Regular Expression Extractor . These two values need to be sent in POST request to service provider. Refer below image to see how to do this. We will get an HTML login page as a response to the request sent in 1st step. We need to fetch values of some hidden elements to pass it in the next request. We can do this b

JMeter Exceeded Maximum Number of Redirects Error Solution

While running performance test, JMeter allows maximum 5 redirects by default. However, if your system demands more than 5 redirects, it may result in JMeter exceeded maximum number of redirects error. In this post, we have listed down steps to overcome this error. Actual error in JMeter: Response code: “Non HTTP response code: java.io.IOException” Response message: “Non HTTP response message: Exceeded maximum number of redirects: 5” This error is noticed because  JMeter  allows maximum 5 redirects by default and your system may be using more than 5 redirects. You need to increase this count to more than 5 in jmeter.properties file. Follow below steps to achieve this. Navigate to /bin directory of your JMeter installation. Locate jmeter.properties file and open it in any editor. Search for “httpsampler.max_redirects” property in opened file. Uncomment the above property by removing # before it. Change to value to more than 5 Eg. 20. Save the file and restart JMeter. If

VBScript Code - Function to convert CSV file into excel and viceversa in QTP using VBScript

We at times are required to convert excel files into csv to read as flat files and sometime require to convert a csv file into excel file to use excel features on the data.   Below function shows how to convert an csv file into excel file and vice versa. We can also convert to other formats based on constants Here constant value 23 is used to create a csv file and constant -4143 to save a file as xls file. Once the destination file is created, we can delete the source file as shown below.  In case of any issue in understanding the code, please add in comment section Call func_ConversionCSVExcel("E:\Test.csv", "E:\Test_converted.xls", "csvtoexcel") Public Function func_ConversionCSVExcel(strSrcFile, strDestFile, Conversion) on error resume next Set objExcel = CreateObject("Excel.application") set objExcelBook = objExcel.Workbooks.Open(strSrcFile) objExcel.application.visible=false objExcel.application.displayalerts=