VBScript: Strings, You Can`t Function without Them - Trim()ming the Fat
(Page 3 of 6 )
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..."
document.write(Replace(example,"far","near",10,1))
</script>
</body>
</html>
The result:
near, far, far, far, I mean really far away...
And if we want to replace two instances of a given term, here is how we do it:
<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,2))
</script>
</body>
</html>
Here is the print out:
In a land near, near, far, far, I mean really far away...
Next: Comparing Strings >>
More Windows Scripting Articles
More By James Payne