ITworld.com
  Search  
ITworld Home Page ITworld Webcasts ITworld White Papers ITworld Newsletters ITworld News ITworld Topics Careers ITworld Voices ITwhirled Changing the way you view IT
Building Strings for Responses
ASP TUTOR --- 08/13/2001

The following snippet of code provides a simple approach to displaying the contents of the variables "verb" and "noun" within a single sentence. 

On this topic

<%
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.

 





Advertisements
Sponsored links
Locate Hidden Software on business PCs with this free tool
Bring harmony to your mix of UNIX-Linux-Windows computing environments
Top 5 Reasons to Combine App Performance and Security
KODAK i1400 Series Scanners stand up to the challenge
 Home   Newsletters  ASP TUTOR
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Industry Standard   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.