String functions in vbscript with examples

String Functions

InStr : Returns the position of the first occurrence of one string within another. The search begins at the first character of the string
Syntax: InStr([start, ]string1, string2[, compare])
Example: 
Str="How do you DO ?”

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 : Returns the position of the first occurrence of one string within another. The search begins at the last character of the string
Syntax: InStrRev(string1, string2[, start[, compare]])
Example: 
Str="How do you DO ?"

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 

LCase : Converts a specified string to lowercase
Syntax: LCase(string)
Example: 
Print  Lcase("Good Morning")  
'Output --> good morning

UCase : Converts a specified string to uppercase
Syntax: UCase(string)
Example: 
Print  Ucase("Good Morning")             
'Output --> GOOD MORNING
Left : Returns a specified number of characters from the left side of a string
Syntax: Left(string, length)
Example: 
Str="Welcome to the World of QTP"

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

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

Right : Returns a specified number of characters from the right side of a string

Syntax: Right(string, length)

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

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

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

Mid : Returns a specified number of characters from a string
Syntax: Mid(string, start[, length])
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
LTrim : Removes spaces on the left side of a string

Syntax:  LTrim(string)

Example: 

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


RTrim : Removes spaces on the right side of a string
Syntax:  RTrim(string)

Example: 

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




Trim: Removes spaces on both the left and the right side of a string

Syntax:  Trim (string)

Example: 

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

Replace: Replaces a specified part of a string with another string a specified number of times
Syntax: Replace(expression, find, replacewith[, start[, count[, compare]]])
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 : Returns a string that consists of a specified number of spaces

Syntax: Space(number)

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

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

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


StrComp : Compares two strings and returns a value that represents the result of the comparison
Syntax: StrComp(string1, string2[, compare])
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

String : Returns a string that contains a repeating character of a specified length
Syntax: String(number,character)
Print (String(4,"*"))
'Output-->“****”


StrReverse : Reverses a string
Syntax: StrReverse(string1)
Example: 
Str="Welcome to the World of QTP"

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

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

Split : Returns a zero-based, one-dimensional array that contains a specified number of substrings
Syntax: Split(expression[, delimiter[, count[, compare]]])
Example: 
'Space as Delimiter
Str="Welcome to the World of QTP"

SArray=Split(Str," ")

For  i= 0 to UBound(SArray)

    Print  SArray(i)

Next

'Comma As Delimitter

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

SArray=Split(Str,",")

For  i= 0 to UBound(SArray)

    Print  SArray(i)

Next

Join : The Join function returns a string that consists of a number of substrings in an array.
Syntax: Join(list[,delimiter])
Example: 
days=Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
Str = Join(days,” “)
Otputà Sun Mon Tue Wed Thu Fri Sat

IsNumeric : Returns a Boolean value that indicates whether a specified expression can be evaluated as a number
IsNumeric(expression)
Example :
Var = IsNumeric(12345)
Print Var
Output à True
Var = IsNumeric(“ad345”)
Print Var
Output à False
IsArray : Returns a Boolean value that indicates whether a specified variable is an array
IsArray(variable)
days=Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
Var = IsArray (days)
Print Var
Output à True
IsObject : Returns a Boolean value that indicates whether the specified expression is an automation
IsObject(expression)
Set Xl = CreateObject (“excel.application”)
Var = IsObject (Xl)
Print Var
Output à True
IsEmpty : Returns a Boolean value that indicates whether a specified variable has been initialized or not
IsEmpty(expression)
Dim A
Var = IsEmpty (A)
Print Var
Output à True
IsNull : Returns a Boolean value that indicates whether a specified expression contains no valid data (Null)
IsNull(expression)
Dim A
A= Null
Var = IsNull (A)
Print Var
Output à True

TypeName: Returns the subtype of a specified variable
TypeName(varname)
Dim A
A= “Quill”
Var = TypeName (A)
Print Var
Output à String

  • Byte - Indicates a byte value
  • Integer - Indicates an integer value
  • Long - Indicates a long integer value
  • Single - Indicates a single-precision floating-point value
  • Double - Indicates a double-precision floating-point value
  • Currency - Indicates a currency value
  • Decimal - Indicates a decimal value
  • Date - Indicates a date or time value
  • String - Indicates a character string value
  • Boolean - Indicates a boolean value; True or False
  • Empty - Indicates an unitialized variable
  • Null - Indicates no valid data
  • <object type> - Indicates the actual type name of an object
  • Object - Indicates a generic object
  • Unknown - Indicates an unknown object type
  • Nothing - Indicates an object variable that doesn't yet refer to an object instance
  • Error - Indicates an error

Comments

Popular posts from this blog

ExecuteFile Method vs LoadFunctionLibrary Method

Types In Descriptive Programming

Basic information about MAVEN