In our previous article we left off discussing a few of the string functions in VBScript. We didn’t get very far; of the sixteen available to us, we only covered four. As you will recall, they were the InStr and InStrRev, which let us check to see if a particular character or string was within another string, and the UCase and LCase functions, which let us format a given string to all uppercase or lowercase letters.
Contributed by James Payne Rating: / 5 April 21, 2008
We will pick up in this article by previewing the string functions table, and then dive right into the Left, Right, and Mid functions. So strap on your army boots, assemble your rifles, and get ready to kiss the mud. Your left, your left, your left, right, left, now do the hustle.
Function
What It Does
InStr
Used to return the position of the first occurrence of a string within another string.
InStrRev
Same as InStr only it begins searching from the last character of the string.
LCase
Takes a string and converts it to lowercase.
UCase
Takes a string and converts it to uppercase.
Left
Used to return a number of characters you specify from the left side of a string.
Right
Used to return a number of characters you specify from the right side of a string.
Mid
Used to return a number of characters you specify from a string.
Len
Used to return the number of characters in a string.
LTrim
Used to remove spaces from the left side of a string.
RTrim
Used to remove spaces from the right side of a string.
Trim
Used to remove spaces from both the left and right side of a string.
Replace
Used to replace a part of a string with another string a number of times you specify.
Space
Used to return a string made up of a number of spaces you specify.
StrComp
Used to compare two strings. Returns a value representing the results.
StrReverse
Used to reverse a string.
String
Used to return a string that repeats a character a number of times you specify.
The Left() function is used to return a number of strings you specify from the left hand side of the string. For instance, if my string contains the sentence: “Yo mama so ugly she made an onion cry” and we tell the program to return the first seven characters, it will result in: “Yo mama.” I know, I know. That’s only six characters. VBScript counts spaces as characters also; you'll want to keep that in mind. Here is how it looks in code:
<html>
<body>
<script type="text/vbscript">
document.write(Left("Yo mama so ugly she made an onion cry",7))
</script>
</body>
</html>
Giving you:
Yo mama
You can also use the Left() function on variables as well:
<html>
<body>
<script type="text/vbscript">
dim ugly
ugly="Yo mama so ugly she made an onion cry"
document.write(Left(ugly,10))
</script>
</body>
</html>
Here we dim and assign a value to the variable “ugly” and use Left() to parse and print out the first ten characters, resulting in:
Yo mama so
Note that if you specify more characters than are in the string, it will simply print out the whole string:
The Right() function works like the Left() function except that it takes characters starting from the right hand side. The easiest way to explain it is to show it in action:
<html>
<body>
<script type="text/vbscript">
document.write(Right("Yo mama so ugly she made an onion cry",9))
</script>
</body>
</html>
This code counts back from the end of the sentence, grabs the last nine characters and gives us the result of:
onion cry
Once again, you can also use it on variables, like so:
The Mid() function is also used to parse data from a string. However, unlike the left() and right() functions, it doesn’t technically take from either side. By default, I suppose, it starts on the left side, however you have the option to decide at which position you want to begin taking data. For instance, you could have it start five characters in if you wanted. Here is an example showing Mid() in its default state:
<html>
<body>
<script type="text/vbscript">
ugly="Yo mama so ugly she made an onion cry"
document.write(Mid(ugly,1))
</script>
</body>
</html>
This starts at position one, and prints the entire string:
Yo mama so ugly she made an onion cry
We can specify for it to start after the eighth position (remember to count spaces) like so:
<html>
<body>
<script type="text/vbscript">
ugly="Yo mama so ugly she made an onion cry"
document.write(Mid(ugly,8))
</script>
</body>
</html>
Which gives us:
so ugly she made an onion cry
But what if we only want a certain number of characters? Say we want the first 10 characters. We would use the following code:
<html>
<body>
<script type="text/vbscript">
ugly="Yo mama so ugly she made an onion cry"
document.write(Mid(ugly,1,10))
</script>
</body>
</html>
The first number, 1, tells us where to start. The number 10 tells us how many characters to grab:
Yo mama so
And of course if we wanted to grab fifteen characters but wanted to begin in position eight we would use this code:
<html>
<body>
<script type="text/vbscript">
ugly="Yo mama so ugly she made an onion cry"
document.write(Mid(ugly,8,15))
</script>
</body>
</html>
Giving us:
so ugly she ma
Be careful not to set your beginning position to a number greater than the amount of characters in the string. Try out this code:
You can use Left, Right, and Mid all together if you like, though it might not really be all that practical. Still here is some mind numbing code:
<html>
<body>
<script type="text/vbscript">
dim subliminal
subliminal="Kick that ball will you? Do it yourself!"
document.write(Left(subliminal,1))
document.write(Mid(subliminal,17,3))
document.write(Right(subliminal,10))
</script>
</body>
</html>
In the above code I gave the variable “subliminal” a normal looking value; two sentences you might hear some kids shouting on the playground. However, once you insert my sneaky code and run the program, something else entirely happens…the result:
Kill yourself!
Len()ding Yourself a Hand
The Len() function is used to tell you how many characters are in a string. An easy way to remember the Len() function is to think of the word length, from which the function name is derived. Here it is in use:
<html>
<body>
<script type="text/vbscript">
document.write(Len("He-Man should really purchase himself a pair of pants."))
</script>
</body>
</html>
This counts the amount of characters in our string and displays it:
54
We can, of course, do the same thing with variables:
<html>
<body>
<script type="text/vbscript">
goodidea="He-Man should really purchase himself a pair of pants."
document.write(Len(goodidea))
</script>
</body>
</html>
Giving us the same result as before:
54
Though it might seem redundant, you can also use the Len() function in conjunction with the Right() and Left() functions:
<html>
<body>
<script type="text/vbscript">
goodidea="He-Man should really purchase himself a pair of pants."
a=Len(goodidea)
document.write(Len(goodidea)) & "<br />"
document.write(Left(goodidea,a)) & "<br />"
document.write(Right(goodidea,a)) & "<br />"
</script>
</body>
</html>
This code puts the length of the string in the variable “a,” then tells first the left, then the right, how many characters to print from the string. Since we used Len(), it tells them to print all 54 characters. Here is the result of this code:
54 He-Man should really purchase himself a pair of pants. He-Man should really purchase himself a pair of pants.
The last function we will cover in this article is the Space() function, which allows you to return a string with a given number of spaces:
<html>
<body>
<script type="text/vbscript">
dim txt
txt=Space(19)
document.write(txt)
</script>
</body>
</html>
This prints out nineteen spaces. It can be used for a number of purposes, such as formatting, etc.
Well that’s all the time and space we have for this episode. We still have seven more string functions to go, which we should wrap up in our next article. So be sure to stop on by.