<%
Response.write "<H3>It would be a pleasure to "
Response.write verb
Response.write " your "
Response.write noun
Response.write ".</H3>"
%>
If "verb" and "noun" contained the values "drive" and "car",
respectively, the page output would appear as:
It would be a pleasure to drive your car.
While this is perfectly valid code, it breaks a simple sentence up into
five pieces. This makes it a little difficult to read and results in
five function calls where only one is needed.
A more readable and efficient solution would be to rewrite the code as:
<%
Response.write "<H3>It would be a pleasure to " & verb & "
your " & noun & ".</H3>"
%>
The "&" operator, in VBScript, allows you to intermingle script level
code ("<H3>It would be a pleasure to ") with VBSCRIPT local variables
("verb"). In this case, readability of the code is improved; but, more
importantly, the number of calls to "Response.write" is reduced from
five to one. This greatly improves the efficiency of the code and, if
this method is used throughout your pages, it will improve the overall
speed of your web site.
Some developers prefer to prepare the output string in a separate
variable before sending it to the browser output. This allows for
further manipulation of the string before output and adds a little more
to the readability of the code. In this case, we have prepared the
string "MyString" and later sent it to the browser.
<%
MyString = "<H3>It would be a pleasure to " & verb & " your " &
noun & ".</H3>"
Response.write MyString
%>
VBScript provides a few simple functions to help manipulate your
character data. Consider the following string. It contains a mixture
of upper and lower case letters and has two leading spaces and one
trailing space.
MyString = " ITWorld "
The following are some of the functions available for quick
manipulation
of the string.
Response.write UCase(MyString)
The resulting output is the upper-case version of the string:
" ITWORLD ".
Response.write LCase(MyString)
The resulting output is the lower-case version of the string:
" itworld ".
Response.write Trim(MyString)
The resulting output removes leading and trailing spaces from the
string: "ITworld".
Response.write LTrim(MyString)
The resulting output removes leading spaces from the left side of the
string: "ITworld ".
Response.write RTrim(MyString)
The resulting output removes trailing spaces from the right side of the
string: " ITworld".
If we re-examine our code above, we can add some of our string
manipulation functions to help ensure that our output looks good on the
screen. Assume that "verb" and "noun" were provided through form
fields and held the values " Drive " and "CAR ", respectively. Our
code would produce the output:
It would be a pleasure to Drive your CAR .
Using the simple tools discussed above, we could rewrite our code as:
<%
Response.write "<H3>It would be a pleasure to " &
LCase(Trim(verb)) & " your " & LCase(Trim(noun)) & ".</H3>"
%>
This new code provides the following output:
It would be a pleasure to drive your car.
Notice that I have combined the use of two of the functions, "UCase"
and "Trim". The code "LCase(Trim(verb))" starts at the innermost set
of parentheses and works its way out. In this case, the leading and
trailing spaces are removed before the resulting string is reduced to
lower-case.