VBScript: More String Functions - Left, Right, Mid…Using Them in a Stupid Way
(Page 5 of 6 )
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.
Next: Space() Not Really the Final Frontier After All >>
More Windows Scripting Articles
More By James Payne