Using built-in functions in ASP

Type Checking

These functions allow you to figure out data types.

TypeName()
Returns the data type rather than the code.

IsNumeric()
Returns a boolean value of true if the data type is that of a number and false if otherwise.

IsArray()
Returns a boolean value of true if the data type is that of an array and false if otherwise.

IsDate()
Returns a boolean value of true if the data type is that of a date and false if otherwise.

IsEmpty()
Returns a boolean value of true if the data type is a empty value and false if otherwise.

IsNull()
Returns a boolean value of true if the data type contains no valid data and false if otherwise.

IsObject()
Returns a boolean value of true if the data type is that of an object and false if otherwise.

Typecasting

What’s typecasting? Typecasting converts between data types. For instance, if you have data you obtained from a form, it’s considered text. However, with typecasting, you can convert that variable into a date value.

Dim idate
‘Get the DOB from the form
idate = Request.Form(“DOB”)
‘Convert it to a date
idate = cdate(idate)

With the CBool() function, you can get a boolean value. If the number is 0, you get a boolean value of false, otherwise you get a boolean value of true. The following code will return true.

Dim inumber
inumber = 12
If CBool(inumber) = True Then
Response.Write “True”
Else
Response.Write “False”
End If

Formatting Functions

Formatting functions are useful when you need to display data the way you want it. For results, you will end up with a string. The code below will return 4/6/2004

Response.Write FormatDatetime(date, vbshortdate)

You can also display the date like this: Tuesday, April 06, 2004, using the following code:

Response.Write FormatDatetime(date, vblongdate)

The same applies to time:

Response.Write FormatDatetime(time, vblongtime)

Math functions

Vbscript has lot’s of math functions, however, you won’t use them very often, so I will only list a couple.

Rnd(), when used with randomize, will generate an random number (random enough) less the one and greater than or equal to zero. Try out the following code:

randomize
Response.Write rnd

What does randomize do? Randomize uses the system timer to start the random number generator.

Another vbscript math function you might run into is the Round() function. And as you would guess, it rounds numbers.

Round(9.99)

The code above returns 10.

Date Functions

When working in vbscript, you will come across dates and their values. Luckily, vbscript as a ton of built-in functions just for this.

‘add a month
Response.Write DateAdd(“m”, 1, #12-09-1981#)
‘add a year
Response.Write DateAdd(“yyyy”, 1, #12-09-1981#)
‘add a day
Response.Write DateAdd(“d”, 1, #12-09-1981#)

As you can see, one month, year and day is added to the dates. Remember to add the # character at the beginning and the end of a date. This tells vbscript to treat the following string as a date and not as a math problem: 12/09/1981 = 12 divided by 09 divided by 1981

If you want to figure out the number of days, month or years between to dates, you can use the DateDiff() function.

DateDiff(“yyyy”, #12-09-1981#, #12-09-1982#, )

The code above returns 1, since there’s one year between 12-09-1981 and 12-09-1982.

If you need to obtain a value from a variable and place it into an existing date, you can use the DateSerial() function.

Dim iday
iday = 12
Response.Write DateSerial(year(date), month(date), iday)

The following code takes the variable iday and places it into the present month and year to create a date. As you can see from above, date returns the present date.

To get the year of the present date, use the following code:

Response.Write year(date)

There’s also the TimeSerial() function, which is just like the DateSerial() function, except it uses times not dates.

Now what if you want to only get a certain part of a date? You can use the function DatePart().

DatePart(“m”, #12-09-1981#)

The code above will return 12 as you assumed.

If you want to get the month name instead of a number, you can call the MonthName() function.

Response.Write monthname(12)

The code above returns December. To abbreviate this, use the argument abbrev.

Response.Write monthname(12, true)

To get the name of the present month, do the following:

Response.Write monthname(month(date), true)

String Functions

Probably the most useful functions in vbscript would be the string functions. And there’s a lot of them.

If you need text in all upper/lower case characters, use the UCase() or LCase() functions:

Response.Write UCase(“codehungry”)
Response.Write LCase(“CODEHUNGRY”)

LTrim() and RTrim() remove blank spaces from the left and right of a string.

Response.Write LTrim(mytext)
Response.Write RTrim(mytext)

And of course, Trim() removes spaces from both sides.

Space() will give you any number of spaces you specify:

Response.Write Space(12) & “Start my text here!!!”

String() will give you any number of characters you specify:

Response.Write String(12, “$”)

Len() returns the number of characters in a word

Response.Write Len(myvariable)

The StrComp() function is used to compare to strings:

Dim mytext1, mytext2
mytext1 = “Andrew”
mytext2 = “Jennifer”
Response.Write StrComp(mytext1, mytext2, 1)

The following code would return -1 because mytext1 is less than mytext2. If you were to reverse the two strings, you would get a value of 1 because mytext1 would be greater than mytext2. If the strings where the same, you would get a result of 0.

The last argument is the comparetype argument. If this argument is 1, then the two strings are compared texturally. If the comparetype argument is set to 0, then the two strings are compared binaurally. For instance, Apple and apple would be considered the same word if they were compared texturally. If they where compared binaurally, the would be considered different.

The Right() and Left() functions will return the number of characters from a string that you specify starting in that direction.

Response.Write Left(“Andrew Schools”, 6)
Returns Andrew

Response.Write Right(“Andrew Schools”, 8)
Returns Schools

The Mid() function works almost like the Right() and Left() function. For an example:

Response.Write Mid(“Andrew Schools”, 1, 6)
Returns Andrew

Response.Write Mid(“Andrew Schools”, 8, 15)
Returns Schools

The Instr() function is a very useful and powerful function. It is used to check where string1 is within string2 and gives you a numeric value.

Instr(start, string1, string2, compartype)

As you can see from above, the start argument lets you specify where in string1 you want to start. And remember comparetype is used to compare the text texturally or binaurally. The code below will find the letter a.

Dim mytext
mytext = “ActiveServerPages”
Response.Write Instr(1, mytext, “a”, 1)

The code above would return 1

Dim mytext
mytext = “ActiveServerPages”
Response.Write Instr(2, mytext, “a”, 1)

The code above returns 14

And than there’s the InstrRev() function which works just like the Instr() function except it starts looking for the match starting from right to left.

The Replace() function is also a very useful and powerful function. It allows you to search for a string within a string and replace it with whatever your heart my desire. The Replace() function takes the following arguments. They are self explanatory.

Replace(string1, find, replace, start, count, comparetype)

To use the Replace() function, try the code below.

Dim mytext
mytext = “Try to find the dog”
mytext = Replace(mytext, “dog”, “cat”, 1, -1, 1)

The code above will find word dog and replace it with the word cat.

The filter() function searches an array. It includes the following arguments:

Filter(array, searchfor, include, comparetype)

The include argument is boolean value. If it’s set to true, it will find all strings containing the character from searchfor. If it’s set to false, then filter() will return all strings not containing the character searchfor.

Dim myarray, arr
myarray = array(“Andrew”, “Jennifer”, “Jessie”)
myarray = Filter(myarray, “j”, True, 1)
For Each arr in myarray
Response.Write arr & “<BR>”
Next

The code above searches the array myarray for words that have the letter j in it. There is a little more to this code than just that. You must use a For…Next loop to retrieve the values from the array.

Split() will take a string and split it up into an array. The Split() function takes the following arguments:

Split(expression, delimiter, count, comparetype)

Expression is the string you want to split up. Delimiter is what you want to separate the new strings. The code below takes a string and makes it an array:

Dim mytext
mytext = “Andrew;Schools;Code;Hungry”
mytext = Split(string1, “;”, -1, 1)

And if you wanted to retrieve those strings from that array, you need a For…Each loop.

Dim mytext
mytext = “Andrew;Schools;Code;Hungry”
mytext = Split(mytext, “;”, -1, 1)
For Each arr in mytext
Response.Write arr & “<BR>”
Next

Join() is the opposite if Split(). It takes an array and makes one string:

Dim myarray, string1
myarray = Array(“Andrew”, “Schools”, “Code”, “Hungry”)
string1 = Join(myarray, “;”)
Response.Write string1

Returns “Andrew;Schools;Code;Hungry”

Since we are on the subject of arrays, the functions LBound() and UBound() come in handy. If you want all the strings in an array, you can do the following:

Dim myarray, iloop
myarray = Array(“Andrew”, “Schools”, “Code”, “Hungry”)
For iloop = LBound(myarray) to UBound(myarray)
Response.Write myarray(iloop) & “<BR>”
Next

As you can see, LBound() returns the smallest index (usually zero) while UBound() returns the largest index.

When you use the keyboard, the computer sees numbers while you see letters. For instance, the letter A is really 65 and a is 97. Using the ASC() function, you can figure out what any letter converts to:

ASC(“A”)

Which returns 65

Or you can use the CHR() function to convert the number back to the character is represents:

CHR(“65”)

Which returns A

To list the entire alphabet, you can use a For…Next loop with the ASC() and CHR() functions:

For iloop = ASC(“A”) to ASC(“Z”)
Response.Write CHR(iloop) &vbcrlf
Next

Which will return A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Author: Andrew Schools