Академический Документы
Профессиональный Документы
Культура Документы
String Functions
VBScript has several String functions for handling data that come from
outside your script. In our case, that data comes from a web page. In other
courses, the data may come from command line parameters.
Input from a user comes to your VBScript via an HTML form. Users being
people, they often do not input clean data: the input can have leading
and/or trailing blanks, it could be in mixed case. The user knows what they
mean. To your program, it's just a bunch of characterswrong characters
that will mess up your database inquiries and updates.
Imagine some not-so-good input in a variable:
text = "
aBcDe
"
VBScript has String functions you should always use before dealing with user
input:
Trim(string) returns a string with leading and trailing spaces removed.
Lcase(string) returns all characters in lower case
or Ucase(string) returns all characters in UPPER CASE
text = Trim(text) ' trim spaces
text will now contain "aBcDe"
text = Lcase(text) ' ensure all lower case
text will now contain "abcde"
text = Ucase(text) ' ensure all UPPER case
text will now contain "ABCDE"
Now, what if the input was all spaces? They will all be trimmed away.
Check that the user input something by checking the length of a variable's
contents:
Len(variable) returns the number of positions (characters or digits) in a
variable. The most efficient way for VBscript to check if a variable is empty is
to check if its length is zero. (Checking for an empty string "" also works but
1 of 13
LAB5a.asp
Try all this out using Lab 3. Copy Lab 3 to Lab5a.asp. Use the above
functions to standardize the user's input of the Diploma Code.
The practice is to
1) retrieve the value from the HTML form into a variable,
2) replace the variable with the trimmed and same case value,
3) return the standardized input back to the HTML INPUT field.
Using the above examples, the code would be:
text = Request.Form("text") ' retrieve input from an HTML field called "text"
text = Trim(text) ' replace without leading/trailing spaces
text = Ucase(text) ' replace with UPPER CASE characters
Hard core programmers might code it this way:
text = Ucase(Trim(Request.Form("text")))
-------------------==========================
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nested functions are resolved from the inside out. Request.Form("text")
returns a string to the Trim function. Trim returns a string to the Ucase
function. Ucase returns a string which is assigned to the text variable.
Numeric Functions
Check for numeric input and Convert it
Forms return only string data types to your script. If the user is supposed to
input a number, use the built-in function IsNumeric() to test if a string
could be evaluated as a number. Then you can safely convert that variable
into a real number using Ccur() for rounded currency (4 accurate decimals),
2 of 13
currency, accurate to 4
decimals
USE THIS TYPE
-922,337,203,685,477.5808 to
922,337,203,685,477.5807
dbl - Double
double-precision floatingpoint.
These decimal position
are approximate!
Do not use for money!
Sadly, this is usually the
default numeric type
used by VBscript.
lng - Long
big integer
-2,147,483,648 to 2,147,483,647
-32,768 to 32,767
Note that the IsNumeric() function does not actually determine whether an
expression contains only numeric digits; it checks if the expression could be
evaluated as a number by the Cxxx conversion functions. Numeric
punctuation such as $,.- is allowed as are leading/trailing spaces.
The conversion functions ignore leading and trailing spaces and consider a
variable without a value to be zero. An empty string or only spaces, such as
might be submitted from a web page, is not numeric and will result in an
error when converted.
percentage = "
-13.0
"
IsNumeric(percentage) ' returns TRUE
But percentage is still a string.
3 of 13
4 of 13
Lab 5a continued
Use the Round() or the FormatNumber() function to output a whole number
when showing the percentage of D grades. e.g. change the output from this:
Computer Programmer Diploma has 21 courses.
Student with 42.8571428571429% D grades is NOT eligible to graduate.
to this:
Computer Programmer Diploma has 21 courses.
Student with 43% D grades is NOT eligible to graduate.
5 of 13
6 of 13
7 of 13
string_variable is examined from left to right for the "search" string. The
number of the "search" string's beginning position within the
string_variable is returned. If "search" is not found, zero is returned.
text = "Microsoft"
position = InStr(text, "o")
InStr returns 5. Only the first "o" is found examining from left to right.
There is an option telling InStr to begin looking from other than the 1 st
position.
InStr(start_position, string_variable, "search")
The "search" is begun at the start position.
If "search" is not found, zero is returned.
Example: Find the "o" inside "Microsoft"
text = "Microsoft"
position = InStr(6, text, "o")
InStr begins searching at the 6th position within the search string; it returns
7. The SECOND "o" starts at the 7th character.
To find both "o" characters, the above search could be written:
text = "Microsoft"
searchText = "o"
position = InStr(text, searchText)
IF position = 0 THEN ' search unsuccessful
Response.Write searchText & " not found in text "
ELSE
' character was found
Response.Write searchText & " found in position " & position
' check if searchText is found again starting one position
beyond the first
IF InStr(position + 1, text, searchText) > 0 THEN ' 2nd
searchText found
Response.Write "Next " & searchText & " found in position "
& position
8 of 13
9 of 13
Replacing Text
Like the find and replace feature in editors, scripts may need to replace
characters in a string. The Replace() function allows us to do just that. The
syntax of this function is:
10 of 13
LAB5b.asp
Use the Lab5b_template.asp from the web page for the next task. Lab5b
takes a full name as input, validates, and parses it into Family & Given
names.
GATES
WILLIAM H
[add leading/trailing spaces and spaces before/after the comma and within names]
[try input with and without one or two commas]
Click to SUBMIT the above data
Input standardization
Retrieve the Full Name from the form, Trim it of leading and trailing spaces,
and convert the data to upper case.
Name validation
Verify that Full Name variable contains "," (a comma). Issue a message if a
comma was not found or another message if a second comma was found.
Use the InStr and InStrRev functions.
11 of 13
Processing
If input is valid, format a message to be output to the user reporting the
Family and Given names. See the sample above.
,,
12 of 13
a ,
a ,
aa
c
,
dd
aa
bb ,
cd dd
aa
bb ,
cc
aa bb ,
aa
, c
cc dd
dd
Family: AA Given: C
Date:
__________________
13 of 13