In this continuing series on VBScript functions, I will cover the string functions. In the past, we went over date, time, and array functions. There are not quite as many built-in string functions in the language as there are date/time functions, but what it lacks in quantity, it more than makes up for in quality.
Contributed by James Payne Rating: / 2 April 14, 2008
So enough blabbering on about it. Take a look at the following table, admire the cold black borders, the bold text, and most importantly, the archaic definitions, which are sure to baffle your feeble minds.
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 lower case.
UCase
Takes a string and converts it to upper case.
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 the 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 the number of times you specify.
The InStr() function can be used for a number of purposes. For starters, you can use it to see if a particular string contains another character or string, as in the following sample:
<html>
<body>
<script type="text/vbscript">
dim toon
dim where
toon="George, George, George of the jungle."
where=InStr(1,toon,"j",0)
document.write(where) & "<br />"
where=InStr(toon,"of")
document.write(where)
</script>
</body>
</html>
In the above sample we assign a value to the variable toon, then use the InStr() function to check whether the character “j” is in the string. It then prints the position at which the first occurrence of “j” appears. Next we use InStr() to check whether the string “of” is in the toon variable. Once more, we print the position of the first occurrence of, well…”of”. Here is the result:
31
24
This means if you count 31 characters (including spaces) into the variable, you will find “f”. Likewise, if you count 24 characters in, you will find the first occurrence of the word “of”.
But what if a character or string is not found? Observe this code:
<html>
<body>
<script type="text/vbscript">
dim toon
dim where
toon="George, George, George of the jungle."
where=InStr(1,toon,"o",0)
document.write(where) & "<br />"
where=InStr(toon,"forest")
document.write(where)
</script>
</body>
</html>
Here we search for the character “o” and the string “forest”. The character “o” is found a number of times, but remember that InStr() only returns the position of the first occurrence. The string “forest” is not found, and so a “0” is returned.
Here is the result:
3
0
In all of the above examples, we begin our search with the first character in the toon variable. There is an option with the InStr() function that lets you decide from which position to begin searching. For instance, if we wanted to begin our search after the first word “George,” we would start the search position at 6, like so:
<html>
<body>
<script type="text/vbscript">
dim toon
dim where
toon="George, George, George of the jungle."
where=InStr(6,toon,"o",0)
document.write(where) & "<br />"
where=InStr(toon,"forest")
document.write(where)
</script>
</body>
</html>
As you can see, the program now returns the first occurrence of “o” at position 11, instead of 3, as in our previous example.
Here is the result:
11
0
You may also have noted in the above code (where=InStr(6,toon,"o",0) that after the criteria “o”, there was a 0. This is an optional piece of code that lets you specify whether to do a binary (0) or textual (1) search.
You can also test to see whether or not the value in a variable is in a string and have it print the position where the value begins, like so:
<html>
<body>
<script type="text/vbscript">
dim ape
dim toon
dim where
toon="George, George, George of the jungle."
ape="the"
where=InStr(1,toon,"o",0)
document.write(where) & "<br />"
where=InStr(toon,ape)
document.write(where)
</script>
</body>
</html>
This code will result in:
3
27
Working with the InStrRev() Function
As you can imagine, the InStrRev() function works like the InStr() function, only in reverse. It begins searching from the end of the string and works its way to the front. Here are some examples showing how it works:
<html>
<body>
<script type="text/vbscript">
dim gump,where
gump="Ugly is as ugly does"
where=InStrRev(gump,"ugly")
document.write(where)
</script>
</body>
</html>
The result of this code is:
12
If we had used the InStr() function, it would have found the first instance of the word “ugly” at position 1, or at the beginning of the string. However, with the InStrRev() function, we begin searching at the end of the string and thus, find the first occurrence of “ugly” at position 12 instead.
And just as you can specify the location for the search to begin with InStr(), so too can you using InStrRev:
<html>
<body>
<script type="text/vbscript">
dim gump,where
gump="Ugly is as ugly does"
where=InStrRev(gump,"ugly",19)
document.write(where)
</script>
</body>
</html>
Here, we begin the search at the nineteenth position and we again find the first occurrence of “ugly” at position 12.
We use LCase() and UCase() to make a string lower case or upper case, respectively. Here is the LCase function at work:
<html>
<body>
<script type="text/vbscript">
dim small
small="LOOK HOW BIG I AM!"
document.write(LCase(small))
</script>
</body>
</html>
Even though the value in the variable “small” starts out fully capitalized, the result is:
Look how big I am!
You will note that you do not have to use a variable in order to use the LCase() function:
<html>
<body>
<script type="text/vbscript">
document.write(LCase("I AM A GIANT!"))
</script>
</body>
</html>
This prints out:
i am a giant!
The UCase() works in a similar fashion, only it makes everything upper case:
<html>
<body>
<script type="text/vbscript">
document.write(UCase("I am a giant!"))
</script>
</body>
</html>
The result:
I AM A GIANT!
If it sees that a letter is already upper case, it does not change it.
You can also use UCase() on variables as well:
<html>
<body>
<script type="text/vbscript">
dim giant
giant="i am a giant!"
document.write(UCase(giant))
</script>
</body>
</html>
Again we have:
I AM A GIANT!
And finally, we can also mix the UCase() and LCase() together, like this:
<html>
<body>
<script type="text/vbscript">
dim giant
giant="i am a giant!"
document.write(UCase(giant)) & "<br />"
document.write(LCase(giant))
</script>
</body>
</html>
This will print out:
I AM A GIANT!
i am a giant!
One last thing on the UCase and LCase functions: you will note that the actual value of the object you use the functions on does not change. Observe this example:
<html>
<body>
<script type="text/vbscript">
dim giant
giant="i AM a giant!"
document.write(UCase(giant)) & "<br />"
document.write(LCase(giant)) & "<br />"
document.write(giant)
</script>
</body>
</html>
The print out is:
I AM A GIANT!
i am a giant!
i AM a giant!
As you can see, we printed out the value in “giant” to show that it remains unchanged, despite using the functions on it.
Conclusion
There are sixteen built-in functions for dealing with strings in VBScript and we have only touched upon four of them in this article. In the next tutorial we will cover more, if not all, of them. And in the weeks to come, we'll eventually conclude our discussion of functions altogether. So be sure to check back often.