|
|
 |
|
Lesson 10: Master your strings! |
|
Lesson 10: Master your strings!
 |
Author: Alexander Haneng
Difficulty: Medium
Requires: ASP
Demo: n/a
Download: n/a
|
|
|
Summary:
Master VBScript strings.
Intro:
So code warrior, think you master VBScript after lesson 5??
How about those strings? VBScript has got a good set of
tools for dealing with text strings, and you often have too.
You get allot of text strings from forms and databases, now comes
the time to search them, replace them, build them, compare them, extract them...
Building
You can join text strings with the & sign.
E.g. "This is a short and" & " meaning less sentence" will
join the two strings into "This is a short and meaning less sentence".
You can also join text with variables. E.g. "The time is " & time & " right now"
will give you this sentence: "The time is 11:53:05 PM right now".
You can also join arrays:
<%
DIM MyArray(2)
MyArray(0) = "This is "
MyArray(1) = "a little "
MyArray(2) = "sentence."
Sentence = JOIN(MyArray)
%>
The output of sentence would be:
"This is a little sentence."
Extract
To extract a part of the content of a string, use Left, Mid or Right.
Left returns a certain number of characters counting from the left.
The Right function is identical with Left, except it counts from the right.
The Mid function returns the part starting at the specified number of places from the left,
and stopping after the specified length.
Left("This is a little sentence.",4) returns "This"
Right("This is a little sentence.",16) returns "little sentence."
Mid("This is a little sentence.",6,4) returns "is a"
See also Split and Filter
Replace
One of the most useful functions is the replace function.
You can use it to locate and replace a part of one string with another
string.
E.g. to replace all the occurrences of # in a string with !, you just
write:
Replace("This is a good site# It rules#","#","!") returning "This is a good site! It rules!"
For more info on Replace, go to reference: replace
Search
To search a string for the first occurrence of another string, use
the InStr function, it will return the position of the string.
InStr("Hi there stranger!","there") returns 4
InStr("Hi there stranger!","THERE") returns 0
InStr will hunt for the first match, but you can specify
to start hunting from a specified position.
InStr("There is some one there, is it not?","is") returns 7
InStr(8,"There is some one there, is it not?","is") returns 26
Compare
To compare two strings, you can use = or StrComp.
= is case sensitive, e.g. "peach" = "PEACH" is false.
Use LCase or
UCase to work around this:
UCase("peach") = UCase("PEACH") is true.
The StrComp is used like this:
StrComp(string1,string2) and returns 0 if they are the same (else 1).
StrComp is also case sensitiv, but you can add a parameter to
change this: StrComp(string1,string2,1)
Other useful functions when dealing with strings
Often you get some extra spaces on the left and right of
strings submitted trough forms. Use Trim(string) to remove them.
To remove spaces only to the right or left of the string, use
LTrim and RTrim.
UCase converts all the characters in the string into upper case
(capital letters), and LCase converts all characters to lower case.
E.g. UCASE("Alex") results in "ALEX"
StrReverse is to me a mystery. When would you use such a function?
Anyway, StrReverse reverse the character of the string. E.g.
StrReverse("Have a nice day") returns "yad ecin a evaH".
To see an example on how you can use these functions together, look
at the Format_text code in our code section.
Where to go next:
Take a look at our lessons page
to get an overview of all our lessons.
| |
|
|
 |
|