VBSCRIPT STRING FUNCTIONS

STRING FUNCTIONS:

  1. InStr-returns the position of the first occurrence of one string within another.
Syntax: InStr([start,]string1,string2[,compare])
Start-Optional. Specifies the starting position for each search. The search begins at the first character position (1) by default. This parameter is required if compare is specified
String1-Required. The string to be searched
String2-Required. The string expression to search for
Compare-Optional. Specifies the string comparison to use. Default is 0
Can have one of the following values:
  • 0 = vbBinaryCompare - Perform a binary comparison
  • 1 = vbTextCompare - Perform a textual comparison

Example: txt="Introduction to vbscript!"
          response.write(InStr(txt,"vbscript"))
Output:17
  1. InStrRev-returns the position of the first occurrence of one string within another. The search begins from the end of string, but the position returned counts from the beginning of the string.
Syntax: InStrRev(string1,string2[,start[,compare]])
String1-Required. The string to be searched
String2-Required. The string expression to search for
Start-Optional. Specifies the starting position for each search. The search begins at the last character position by default (-1)
Compare-Optional. Specifies the string comparison to use. Default is 0
Can have one of the following values:
  • 0 = vbBinaryCompare - Perform a binary comparison
  • 1 = vbTextCompare - Perform a textual comparison
Example: txt="Introduction to vbscript!"
         response.write(InStrRev(txt,"vbscript"))
Output:8
  1. Lcase-converts a specified string to lowercase.
Syntax: LCase(string)
Example: txt="INTRODUCTION TO VBSCRIPT!"
response.write(LCase(txt))
Output: introduction to vbscript
  1. Left: returns a specified number of characters from the left side of a string.
Syntax: Left(string,length)
Example: txt="INTRODUCTION TO VBSCRIPT"
          response.write(Left(txt,15))
Output:”INTRODUCTION TO”
  1. Len- returns the number of characters in a string.
Syntax: Len(string)
Example: txt="INTRODUCTION TO VBSCRIPT"
                  response.write(Len(txt))
Output:24
  1. LTrim- removes spaces on the left side of a string.
Syntax: LTrim(string)
Example: fname=" Jaya"
response.write("Hello" & LTrim(fname) & "and welcome.")
Output: HelloJaya and welcome.
  1. RTrim- removes spaces on the right side of a string.
Syntax: RTrim(string)
Example: fname=" Jaya"
response.write("Hello" & LTrim(fname) & "and welcome.")
Output: Hello Jayaand welcome.
  1. Trim- removes spaces on both sides of a string.
Syntax: Trim(string)
Example: fname=" priya"
        response.write("Hello" & Trim(fname) & "and welcome.")
Output: Hellopriyaand welcome
  1. Mid- returns a specified number of characters from a string.
Syntax: Mid(string,start[,length])
String- Required. The string expression from which characters are returned
Start- Required. Specifies the starting position. If set to greater than the number of characters in string, it returns an empty string ("")
Length-optional. The number of characters to return
Example: txt="Introduction to vbscript!"
         response.write(Mid(txt,1,1))
Output:I
  1. Replace- replaces a specified part of a string with another string a specified number of times.
Syntax Replace(string,find,replacewith[,start[,count[,compare]]])
String- Required. The string to be searched
Find- Required. The part of the string that will be replaced
Replacewith- Required. The replacement substring
Start- Optional. Specifies the start position. Default is 1. All characters before the start position will be removed.
Count-Optional. Specifies the number of substitutions to perform.
Default value is -1, which means make all possible substitutions
         Compare- Optional. Specifies the string comparison to use. Default is C
         Can have one of the following values:
  • 0 = vbBinaryCompare - Perform a binary comparison
  • 1 = vbTextCompare - Perform a textual comparison
Example: txt="This is a beautiful day!"
          response.write(Replace(txt,"beautiful","fantastic"))
Output: This is a fantastic day!
  1. Right- returns a specified number of characters from the right side of a string.
Syntax: Right(string,length)
Example- txt="This is a beautiful day!"
         response.write(Right(txt,10))
Output: tiful day!
  1. Space- returns a string that consists of a specified number of spaces.
Syntax: Space(number)
Example: Dim txt
         txt=Space(10)
         response.write(txt)
Output: "          "
  1. StrComp- compares two strings and returns a value that represents the result of the comparison.
Syntax: StrComp(string1,string2[,compare])
String1- Required. A string expression
String2- Required. A string expression
       Compare- Optional. Specifies the string comparison to use. Default is 0
          Can have one of the following values:
  • 0 = vbBinaryCompare - Perform a binary comparison
  • 1 = vbTextCompare - Perform a textual comparison
Example- response.write(StrComp("VBScript","VBScript"))
Output-0
  1. String- returns a string that contains a repeating character of a specified length.
Syntax: String(number,character)
Number- Required. The length of the returned string
Character- Required. The character that will be repeated
Example: response.write(String(10,"#"))
Output: ##########
  1. StrReverse- reverses a string.
Syntax: StrReverse(string)
Example: Dim txt
         txt="This is a beautiful day!"
         response.write(StrReverse(txt))
Output: !yad lufituaeb a si sihT
  1. UCase- converts a specified string to uppercase.
Syntax: UCase(string)
Example: txt="This is a beautiful day!"
         response.write(UCase(txt))
Output: THIS IS A BEAUTIFUL DAY!




No comments:

Post a Comment