Introduction to Strings and Cookies: VBScript inStr, Replace, Mid, and Trim Functions (Tutorial #9)
In my previous tutorial, we discussed ‘Date Functions’ in the VBScript. In this tutorial, I will be covering Strings mainly VBScript inStr and Replace Functions. Check the complete VB Scripting series here.
Both, Strings and Cookies are considered to be important topics in the VBScript. Hence, a clear and proper understanding of both is essential for better programming experiences.
This tutorial gives you a brief overview of Strings and Cookies in the VBScript along with clear and simple examples to enable you to understand them in a better way.
Table of Contents:
Strings & Cookies
The string is one of the different types of data types in the VBScript and is most frequently used while working with the coding part of the scripts. In simple terms, a String is a collection of alphanumeric characters that are clubbed together. Strings can either consist of numbers, characters/special characters, or a combination of all of them.
The cookie is a normal/plain piece of text that the server uses to send data to the browser if a user visits the site. This is mainly used to keep a record or to maintain the information about the session of a user in the browser.
Using Strings in the VBScript
A string is defined with the help of double quotes (“”) in the script. Any variable that is enclosed within the “” is referred to as a String in the script. In simple terms, this is the way to identify a string.
The syntax for this is as follows:
strvar= “Hello”
strvar1=”123456”
strvar2=”%^&*“
strvar3=”H12ab$”
These are all String variables that have a different set of values assigned to them. strvar has all alphabetical values, strvar1 has all numeric values, strvar2 has all special characters and finally, strvar3 includes a combination of all these.
So, a String can be defined in any of the above ways.
Let’s see a Simple Example to understand the usage of Strings in the below script:
<html> <head> <title>Let’s see implementation of a String</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim strname strname = “My name is Ram” Msgbox(strname) </script> </body> </html>
The output is: My name is Ram
Concatenation of Strings in the VBScript
At times, there may be an occasion when 2 or more strings are required to be joined together. This can be achieved with the help of a concatenation operator “&” in the VBScript. With the usage of the ‘&’ operator, any number of strings can be joined together by putting this operator in between the strings to form a big final string as a result.
This is considered to be a very useful operator while working with the Strings.
Let’s see a Simple Example to understand the usage of String Concatenation Operation in the below script:
<html> <head> <title>Let’s see implementation of a String Concatenation</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim strname,strname1,strname2,strname3 strname = “My name is Ram” strname1 = “Hey!! ” & strname strname2 = strname1 & “ and I like” strname3 = strname2 & “ to play cricket!!” Msgbox(strname3) </script> </body> </html>
The output is: Hey!! My name is Ram and I like to play cricket!!
As shown below, strname3 stores the concatenation of all the strings and displays the final output.
In this way, strings can be joined together.
String Functions in the VBScript
There are various string functions that are used in the script to perform the different types of operations on the Strings.
Following is a list of the String functions:
#1) InStr
VBS InStr is used to find the position value of a substring at its first occurrence inside the main string. This function requires 2 strings to be specified to perform this search operation and the search operation starts right from the first character.
The syntax of this function is: InStr(name of string1, name of string2)
If the name of string1 or string2 is null or “” then this function will return null and 0 respectively. In case, if the string is not found then the value of this function will be >=1 and 0.
#2) InStrRev
InStrRev is just the reverse of the above function. This is also used to find the position value of a substring at its first occurrence inside the main string. This function requires 2 strings to be specified to perform this search operation but with a minor difference that the search operation starts from the last character and even the position count starts from the beginning character only.
The syntax of this function is: InStrRev(name of string1, name of string2)
If the name of string1 or string2 is null or “” then this function will return null and 0 respectively. In case, if the string is not found then the value of this function will be >=1 and 0.
#3) LCase
LCase is used to convert the specified string into lowercase.
The syntax of this is: LCase(name of the string)
#4) UCase
UCase is used to convert the specified string into an upper case.
The syntax of this is: UCase(name of the string)
#5) Left
Left is used to fetch/get the mentioned number of characters(as per length parameter) from the left-hand side of the specified String.
The syntax of this is: Left(name of the string, length)
#6) Len
Len is used to get the length of a specified String i.e. the total number of characters of a specified String.
The syntax of this is: Len(name of the string)
#7) StrReverse
StrReverse is used to reverse the specified string i.e. this will return the characters of a specified string in a reverse order starting from the end to the beginning.
The syntax of this is: StrReverse(name of the string)
#8) LTrim
LTrim is used to trim/remove the spaces from the left-hand side of the specified String.
The syntax of this is: LTrim(name of the string)
#9) Trim
Trim is used to trim/remove the spaces from both sides of the specified String.
The syntax of this is: Trim(name of the string)
#10) Right
Right is used to fetch/get the mentioned number of characters(as per length parameter) from the right-hand side of the specified String.
The syntax of this is: Right(name of the string, length)
#11) RTrim
RTrim is used to trim/remove the spaces from the right-hand side of the specified String.
The syntax of this is: RTrim(name of the string)
#12) Mid
Mid is used to fetch the mentioned number of characters from the string by specifying the starting position.
The syntax of this is: Mid(name of the string, starting position)
#13) Space
Space is used to fetch the String containing the required number of spaces as specified inside the parenthesis.
The syntax of this is: Space(number of spaces)
#14) Replace
Replace is used to replace the specified portion of a string with some other text as specified.
The syntax of this is: Replace(name of the string, name of the string to be replaced, name of the new replaced string)
#15) StrComp
StrComp is used to compare the 2 strings and return values on the basis of comparison. This returns 0 if string1 = string2,-1 if string1<string2,1 if string1>string2 and null if any of the strings is null.
The syntax of this is: Replace(name of the string1, name of the string2)
Let’s understand the use of these String Functions with the help of a Simple Example.
<html> <head> <title>Let’s see implementation of String Functions</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim strval,strval1,var1,var2,var3,var4,var5,strval2,var6,var7,var8,var9 strval = “My name is Ram” strval1=” name ” strval2=”My name is Ram” var1=Instr(strval,”Ram”) var2=Ucase(strval) var3=Left(strval,4) var4=Len(strval) var5=trim(strval1) var6=Mid(strval,6) var7=Replace(strval,”Ram”,”Shyam”) var8=Space(6) var9=StrComp(strval,strval1) Msgbox “Position returned by Instr function is ” & var1 & “<br />” Msgbox “Uppercase returned by function is ” & var2 & “<br />” Msgbox “Left characters returned by function is ” & var3 & “<br />” Msgbox “Length returned by Len function is ” & var4 & “<br />” Msgbox “Value returned after Trim function is ” & var5 & “<br />” Msgbox “Value returned by Mid function is ” & var6 & “<br />” Msgbox “New value returned after replacing is ” & var7 & “<br />” Msgbox “String returned by Space function is ” & var8 & “<br />” Msgbox “String Comparison returns ” & val1 & “<br />” </script> </body> </html>
Output is:
The position returned by Instr function is 12
Uppercase returned by the function is MY NAME IS RAM
Left characters returned by the function is My n
Length returned by Len function is 14
Value returned after Trim function is name
Value returned by Mid function is My nam
New value returned after replacing is My name is Shyam
String returned by the Space function is “ “
String Comparison returns 0
So far, we have almost covered all the topics in String.
Working with the Cookies
Each of us must be aware of the term Cookies, let’s discuss how actually cookies work.
Whenever you visit a site or any page, the Server sends data to the browser to get it stored in the browser and it is called a Cookie. And whenever you visit that page or site again, the browser sends that same data which is stored as Cookie to the Server.
Cookies include some variable-length field values comprising 5 fields.
They are:
- Expires field: This includes all the information about the date of expiration of the Cookie. If this field is blank then it means that the cookie would get expired once any visitor quits the browser.
- Domain field: This includes information about the domain name of the site that you visit.
- Path field: This includes the information about the path of the directory or any webpage that actually set the Cookie.
- Secure field: This basically helps in securing the data i.e. if this field is having any value inside the Cookie then this will put a restriction to get the Cookie value retrieved only by the Secure Server.
- Name=Value field: This includes the information about the pair value of a ‘key’ and ‘value’ of a Cookie.
As mentioned, Cookie gets stored automatically. There is an object which stores the cookie property named Document.Object.
Reading and Writing Cookies
document. cookie stores the information of key-value pairs and the expiration date values of a Cookie.
document.cookie = “key1=name of the value1;key2=name of the value2,…….,expires=date”
‘;’ is used to separate the key-value pairs.
Let’s understand how Cookies can be written and read with the help of a Simple Example.
<html> <head> <title>Let’s see method of Reading and Writing a Cookie</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Function writingcookie(name, val) document.cookie = name & "=" & val End Function Function readingacookie () val = document.cookie val1 = split(val,”;”) for i = 0 to ubound(val1) cookiename = split(val1(i),”=”) “key is “ & cookiename (i) & “ and value is “ & cookiename (i+1) Next End Function writingcookie "ram","1234" Msgbox document.cookie & “<br />” Msgbox readingacookie() </script> </body> </html>
Output is:
ram=1234
The Key is ram and the value is 1234
We have seen the important topics in Cookies which would help you to understand better.
Conclusion
VBS Strings and Cookies are one of the important topics in VBScript. I’m sure that this tutorial would have briefed you about the importance and effectiveness of using Strings and Cookies.
VB Scripting series here: My next tutorial will brief you about ‘Events’.
Share your experience of working on Strings and Cookies. Also, let us know if you need some clarification.