VBScript: More String Functions - Left Behind
(Page 2 of 6 )
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:
<html>
<body>
<script type="text/vbscript">
dim ugly
ugly="Yo mama so ugly she made an onion cry"
document.write(Left(ugly,10000))
</script>
</body>
</html>
Our result:
Yo mama so ugly she made an onion cry
Next: Right Ahead >>
More Windows Scripting Articles
More By James Payne