VBScript strings
A String is a grouping of characters sorrounded by double quotes such as "this is a string". With VBScript, you can manipulate strings in several ways such as joining two or more strings, printing the length of a string, and more.
This tutorial focuses on:
- Creating a String
- Printing Strings
- Joining Strings
- String functions
Creating a String
A String can be created by sorrounding text in double quotes and assigning it to a variable.
Example:
<script type="text/vbscript">
Dim aString
aString= "This is a string."
</script>
Printing Strings
A String can be printed by referencing the variable that stores the String by name.
Example:
<script type="text/vbscript">
Dim aString
aString= "This is a string."
document.write(aString)
</script>
Output:
This is a string.
Joining Strings
You can join strings using the "&" (ampersand) operator. By joining strings, you can combine several strings into one or print several strings together at the same time.
Example:
<script type="text/vbscript">
Dim stringOne, stringTwo, statement, color
stringOne = "Here is "
stringTwo = "some text"
statement = "My favorite color is "
color = "green"
'join the statement and color strings into one
'and add another string to this string which is
'the period character
statement = statement & color & "."
'print stringOne and stringTwo together
document.write(stringOne & stringTwo)
'print a line break and the value of the statement variable
document.write("<br />" & statement)
</script>
Output:
Here is some text
My favorite color is green.
String functions
VBScript provides various functions used for working with strings.
- len() - returns the length() of a string
- mid() - returns a specified number of characters from a string. This function takes three parameters - the name of the string to extract characters from, the point in the string where to start extracting characters from, and the number of characters to extract
- ltrim() - removes spaces from the left side of a string
- ucase() - converts a string into all uppercase letters
Example:
<html>
<head>
<title>String functions</title>
</head>
<body>
<script type="text/vbscript">
Dim aString, anotherString
aString = "VBScript is a scripting language."
anotherString = " There are five spaces at the beginning of this string."
document.write("Length of the aString string: " & len(aString))
document.write("<br />The first five characters from the aString string:" & mid(aString,1,5))
document.write("<br />The anotherString string without spaces at the beginning of it: " & ltrim(anotherString))
document.write("<br />The anotherString in all uppercase letters: " & UCase(anotherString))
</script>
</body>
</html>
Output:
Length of the aString string: 33
The first five characters from the aString string:VBScr
The anotherString string without spaces at the beginning of it: There are five spaces at the beginning of this string.
The anotherString in all uppercase letters: THERE ARE FIVE SPACES AT THE BEGINNING OF THIS STRING.