VBScript: Strings, You Can't Function without Them
Welcome back to the third, and hopefully final, chapter in our series on VBScript string functions. So far we have covered nine of the sixteen built-in string functions available. In this article, with any luck, we will get to the remaining seven. So strap in, we have a lot of ground to cover.
Contributed by James Payne Rating: / 2 April 28, 2008
Among those covered were the InStr() and InStrRev(), which allowed us to test whether a character or string was contained within another string, and the LCase() and UCase(), which converted strings to lowercase and uppercase respectively. After that we discussed the Right(), Left(), and Mid() functions, which let us, for lack of a better term, parse out a section of a string. Finally, we covered Len(), which counted the number of characters in a string.
Here and now, we will take a heartbreaking last look at our dear friend the String Function Table, and then learn about the next subject on the block, the Trim() functions. So get out your razors and shaving cream, and get prepared to lose that precious peach fuzz you call a mustache.
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
Let's face it, we all could use a little trimming. On our left side, and well, on our right side too. I mean, if you lopped off like fifty pounds from my left side, I would turn into one of those bopper weeble bags that you punch and it just bounces right back up.
Strings in VBScript can sometimes have the same problem. If you want to get rid of those extra spaces on the left side of a string, then LTrim is your friend. Your skinny, good-looking friend you love to hate. That bastard.
<html>
<body>
<script type="text/vbscript">
dim s
s=" We are the Space Invaders! We have come to...well...invade your space! "
document.write(LTrim(s)) & "<br />"
</script>
</body>
</html>
This deletes the spaces on the left hand side of the string, resulting in:
We are the Space Invaders! We have come to...well...invade your space!
RTrim...Like LTrim But on the Other Side!
As the title says, RTrim() is the same as LTrim, only on the other side. There isn't really much else to say about it, so here it is in code:
<html>
<body>
<script type="text/vbscript">
dim s
s=" We are the Space Invaders! We have come to...well...invade your space! "
document.write(RTrim(s)) & document.write("Look out we've collided with some text because you ate our spaces!")
</script>
</body>
</html>
This gives us the result of:
Look out we've collided with some text because you ate our spaces! We are the Space Invaders! We have come to...well...invade your space!
I know what you are thinking, so hold off on those e-mails. If you want to get rid of spaces on both sides of a string, you can do so with LRTrim(). No wait, no you can't. What were you thinking? You can however, use Trim() by itself. Here is how:
<html>
<body>
<script type="text/vbscript">
dim s
s=" We are the Space Invaders! We have come to...well...invade your space! "
document.write(Trim(s)) & document.write("Look out we've collided with some text because you ate our spaces!")
</script>
</body>
</html>
The result:
Look out we've collided with some text because you ate our spaces!We are the Space Invaders! We have come to...well...invade your space!
Why LTrim, RTrim, and Trim() Don't Actually Seem to Work
You may have noticed when you insert your Trim() functions into HTML they don't quite work as you might think. Or better yet, you may have noticed that you don't really need them. This is because in HTML, extra spaces are automatically deleted. And if you use the technique to make sure your spaces don't get deleted, then, well, it wouldn't make sense to delete those spaces. Is that clear? Wait, where am I?
The Replace() Function
This handy little function is used when you want to replace a portion of a string with another string, a given number of times. Consider the following code:
<html>
<body>
<script type="text/vbscript">
dim example
example="In a land far, far, far, far, I mean really far away..."
document.write(Replace(example,"far","near"))
</script>
</body>
</html>
In this example, we replace all instances of the word "far" with the word "near." Sometimes however we might not want to replace every instance. In these situations, we can use some of the optional components of the Replace() function:
<html>
<body>
<script type="text/vbscript">
dim example
example="In a land far, far, far, far, I mean really far away..."
document.write(Replace(example,"far","near",1,1))
</script>
</body>
</html>
In this example you will notice that we made a change; we added to the expression: document.write(Replace(example,"far","near")) and changed it to document.write(Replace(example,"far","near",1,1)). The first 1 is an optional value you can add, which tells the program to begin in position one of the string (with the first character of the string). The second one is also optional, and tells the program how many times to replace our word. The result of this code is:
In a land near, far, far, far, I mean really far away...
If we change the position of where the replace starts, we get an odd result:
<html>
<body>
<script type="text/vbscript">
dim example
example="In a land far, far, far, far, I mean really far away..."
You can compare strings in VBScript using StrComp(). It does two types of comparisons, a textual and a binary. Binary is the default. If you want to use binary you can either leave it blank or use 0. If you want textual, use -1. Just a brief note here. "WORD" does equal the same value as "word" in a textual search, but does not equal the same thing in a binary search, so always keep this in mind.
This handy function lets you write some text backwards. Here it is folks, what the FCC warned us about all these years:
<html>
<body>
<script type="text/vbscript">
dim txt
txt="!flesruoy lliK syas nataS"
document.write(StrReverse(txt))
</script>
</body>
</html>
The result:
Satan says Kill yourself!
Well, that is it for this tutorial and this series on String Functions in VBScript *throws confetti everywhere.* But never fear, more functions are coming your way.