Use built-in and user-defined functions for data manipulation.
ASP built-in Functions
You will often need to manipulate the values of variables.
VBScript provides a number of functions for this purpose, including a
Replace function that replaces every occurrence of a substring[1] (word or phrase) in another string with a replacement string.
Replace() Function example in ASP
Replace(variable, searchfor, replacewith)
In this code segment, the word "off" is replaced by the word "around" wherever it is found in the text string stored in the variable cTest.
The result can be stored in a new variable or in the original variable.
<HTML>
<% 'BEGIN ASP SCRIPT
Dim cTest, cNew 'Initialize a couple of 'Character variables
cTest = "Just showing off the replace statement"
cNew = Replace(cTest, "off", "around")
%>
<!–END ASP SCRIPT ––>
The new value is <%=cNew%>.
</HTML>
The output of this code segment would be:
The new value is Just showing around the replace statement.
The Trim functions remove leading spaces (LTrim), trailing spaces (RTrim) or both (Trim) from a string.
Be aware that different functions work with different types of data.
ASP Trim() function example
Essential syntax:
LTrim()
RTrim()
Trim()
Here is an example of trimming both leading and trailing spaces from a character string stored in a variable:
<HTML>
<% 'BEGIN ASP SCRIPT
Dim cTest, cNew 'Initialize a couple of
'character variables
cTest = "this is a test"
cNew = Trim(cTest)
%>
<!–END ASP SCRIPT ––>
The new value is <%=cNew%>.
</HTML>
The output from this code segment would be:
The new value is this is a test.
This function could be useful in joining data from multiple fields (first name in one field, last name in another) or a
text field in an HTML FORM in which the user may have put in a blank line or spaces before or after the actual text.
Date and Time Functions
Two of the most useful Date and Time functions are:
Date()
The Date function returns the current date. The date returned is the date on the server.
This value can be stored to a variable and manipulated.
DateAdd(argument,value1,value2)
The DateAdd function adds dates together. It can also be used to increment or decrement a date by an amount in any
time or date unit.
Here is the DateAdd function used to add 30 days to the current date:
<HTML>
.
.
.
<% 'BEGIN ASP SCRIPT
Dim dCurrDate, dNextMonth
'Initialize a couple of date variables
dCurrDate = Date()
'dCurrDate is now equal to today's date
dNextMonth = DateAdd("d", 30, dCurrDate)
'Add 30 days to dCurrDate
%>
<!–END ASP SCRIPT ––>
The new value is <%=dNextMonth%>.
.
.
</HTML>
This MouseOver will give you a line-by-line explanation of how we are using the DateAdd function and what values we need to set in its parameters to add 30 days to the current date.
Several functions that format a number are available to you. FormatCurrency returns the number with a leading currency symbol. FormatPercent returns the number with a following % symbol and multiplies the value by 100 to create a percentage. FormatNumber returns the number itself with no leading or following characters.
The function to format a number as currency has this syntax:
The other numeric functions follow an identical syntax.
You can format the same numeric value in different ways and define some additional information on the functions' parameters.
Numeric functions example
The regional settings on the server determine which currency and grouping symbols are returned by the function.
The valid parameter values for
IncludeLeadingDigit,
UseParensForNegativeNumbers, and
GroupDigits are:
0
False/No
-1
True/Yes
-2
Use the server's regional settings (default)
This example assumes the regional setting is United States.
<HTML>
.
.
<!-- BEGIN ASP SCRIPT -->
<%
Response.Write FormatCurrency(1.231,2) 'Outputs $1.23
Response.Write FormatNumber(1.231,2) 'Outputs 1.23
Response.Write FormatPercentage(1.231,2)'Outputs 123.00%
%>
<!–END ASP SCRIPT ––>
.
.
</HTML>
Response.Write in this example is a form of ASP's Response object.
For now, just think of it as similar to a PRINT statement in BASIC. We'll cover this in more detail in a future lesson.
The ASP statements and functions in these lessons are just a part of what is available for your Web applications' use through VBScript.
Subprocedures
User-defined subprocedures and functions let you reuse code you have already written.
With these "mini-programs", you can create generic code that can be easily adapted from use in one Web site to another.
[1]Substring: A portion of a character string (e.g., "shirt" is a substring of "t-shirt").