通过实例讲解来学习ASP中的函数

80酷酷网    80kuku.com

  

作用:返回一个数组

语法:Array(list)

适用的类型:字符,数字均可

以下为引用的内容:
<%
Dim myArray()
For i = 1 to 7
Redim Preserve myArray(i)
myArray(i) = WeekdayName(i)
Next
%>

结果:建立了一个包含7个元素的数组myArray

myArray("Sunday","Monday", ... ... "Saturday")

CInt()

作用:将一个表达式转化为数字类型

语法:CInt(表达式)

适用的类型:任何有效的字符均可

以下为引用的内容:
<%
f = "234"
response.write cINT(f) + 2
%>

结果:236

转化字符"234"为数字234,如果字符串为空,则返回0值

CreateObject()

作用:建立和返回一个已注册的ACTIVEX组件的实例。

语法:CreateObject(objName)

适用的类型: objName 是任何一个有效、已注册的ACTIVEX组件的名字。

以下为引用的内容:
<%
Set con = Server.CreateObject("ADODB.Connection")
%>
CStr()

作用:转化一个表达式为字符串。

语法:CStr(expression)

适用类型:expression 是任何有效的表达式

以下为引用的内容:
<% 
s = 3 + 2
response.write("The 结果 is: " & cStr(s))
%>

结果: 转化数字5为字符“5”。

Date()

作用:返回当前系统日期。

语法:Date()

适用的类型:None。

<%=Date%>

结果:8/4/99

DateAdd()

作用:返回一个被改变了的日期。

语法:DateAdd(timeinterval,number,date)

说明:timeinterval为所要加入的时间间隔类型;number为要添加的数量;date为起始日期.

以下为引用的内容:

<%  
currentDate = #8/4/99#
newDate = DateAdd(  "m",3,currentDate)
response.write newDate
%>

<%    
currentDate = #12:34:45 PM#  
newDate = DateAdd(  "h",3,currentDate)  
response.write newDate
%>

结果:11/4/99

3:34:45 PM

"m" = "month";
   "d" = "day";

当当前日期格式为time,那么

"h" = "hour";
"s" = "second";

DateDiff()

作用:返回两个日期之间的差值。

语法:DateDiff(timeinterval,date1,date2 [, firstdayofweek [, firstweekofyear >>)

说明:timeinterval 表示相隔时间的类型,如“M“表示“月”。

以下为引用的内容:
<%
fromDate = #8/4/99#
toDate = #1/1/2000#
response.write("There are " & _
DateDiff("d",fromDate,toDate) & _
" days to millenium from 8/4/99."    
%>

结果:There are150daysto millenium from 8/4/99

Day()

作用:返回一个月的第几日。

语法:Day(date)

说明:date 是任何有效的日期。

<%=Day(#8/4/99#)%>

结果:4

FormatCurrency()

作用:返回表达式,此表达式已被格式化为货币值

语法:FormatCurrency(Expression [, Digit [, LeadingDigit [, Paren [, GroupDigit >>>>)

说明:Digit 指示小数点右侧显示位数的数值。默认值为 -1,指示使用的是计算机的区域设置;LeadingDigit 三态常数,指示是否显示小数值小数点前面的零

<%=FormatCurrency(34.3456)%>

结果:$34.35

FormatDateTime()

作用:返回表达式,此表达式已被格式化为日期或时间

语法:FormatDateTime(Date, [, NamedFormat >)

说明:NamedFormat 指示所使用的日期/时间格式的数值,如果省略,则使用 vbGeneralDate.

<%=FormatDateTime("08/4/99", vbLongDate)%>

结果:Wednesday, August 04, 1999

FormatNumber()

作用:返回表达式,此表达式已被格式化为数值

语法:FormatNumber(Expression [, Digit [, LeadingDigit [, Paren [, GroupDigit >>>>)

说明:Digit 指示小数点右侧显示位数的数值。默认值为 -1,指示使用的是计算机的区域设置。; LeadingDigit i指示小数点右侧显示位数的
数值。默认值为 -1,指示使用的是计算机的区域设置。; Paren 指示小数点右侧显示位数的数值。默认值为 -1,指示使用的是计算机的区域
设置。; GroupDigit i指示小数点右侧显示位数的数值。默认值为 -1,指示使用的是计算机的区域设置

<%=FormatNumber(45.324567, 3)%>

结果: 45.325

FormatPercent()

作用:返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 (%)

语法:FormatPercent(Expression [, Digit [, LeadingDigit [, Paren [, GroupDigit >>>>)

说明:同上

<%=FormatPercent(0.45267, 3)%>

结果: 45.267%

Hour()

作用:以24时返回小时数

语法:Hour(time)

说明:

<%=Hour(#4:45:34 PM#)%>

结果:16

Instr()

作用:返回字符或字符串在另一个字符串中第一次出现的位置.

语法:Instr([start, > strToBeSearched, strSearchFor [, compare>)

说明:Start为搜索的起始值,strToBeSearched接受搜索的字符串 strSearchFor要搜索的字符compare 比较方式(详细见ASP常数)

以下为引用的内容:
<%
strText =   "This is a test!!"
pos = Instr(strText,   "a")
response.write pos
%>

结果:9

InstrRev()

作用:同上,只是从字符串的最后一个搜索起

语法:InstrRev([start, > strToBeSearched, strSearchFor [, compare>)

说明:同上.

以下为引用的内容:
<%
strText =   "This is a test!!"
pos = InstrRev(strText,   "s")
response.write pos
%>

结果:13

Int()

作用:返回数值类型,不四舍五入。

语法:Int(number)

说明:

<%=INT(32.89)%>

结果:32

IsArray()

作用:判断一对象是否为数组,返回布尔值.

语法:IsArray(name)

说明:

以下为引用的内容:
<%
strTest =   "Test!"
response.write IsArray(strTest)
%>

结果:False

IsDate()

作用:判断一对象是否为日期,返回布尔值语法: IsDate(expression) 说明: expression is any valid expression.

以下为引用的内容:
<%
strTest =   "8/4/99"
response.write IsDate(strTest)
%>

结果:True

IsEmpty()

作用:判断一对象是否初始化,返回布尔值.

语法:IsEmpty(expression)

说明:

以下为引用的内容:
<% 
Dim i
response.write IsEmpty(i)
%>

结果:True

IsNull()

作用:判断一对象是否为空,返回布尔值.

语法:IsNull(expression)

说明:

以下为引用的内容:
<%
Dim i
response.write IsNull(i)
%>

结果:False

IsNumeric()

作用:判断一对象是否为数字,返回布尔值.

语法:IsNumeric(expression)

说明:

以下为引用的内容:
<%
i =   "345"
response.write IsNumeric(i)
%>

结果:True

就算数字加了引号,ASP还是认为它是数字。

IsObject()

作用:判断一对象是否为对象,返回布尔值.

语法:IsObject(expression)

说明:

以下为引用的内容:
<%  
Set con = Server.CreateObject(  "ADODB.Connection")
response.write IsObject(con)
%>

结果:True

LBound()

作用:返回指定数组维的最小可用下标.

语法:Lbound(arrayname [, dimension >)

说明:dimension 指明要返回哪一维下界的整数。使用1表示第一维,2表示第二维,以此类推。如果省略 dimension 参数,默认值为 1.

以下为引用的内容:
<%
i = Array(  "Monday","Tuesday","Wednesday")
response.write LBound(i)
%>

结果:0

LCase()

作用:返回字符串的小写形式

语法:Lcase(string)

说明:string is any valid string expression.

以下为引用的内容:
<%
strTest =   "This is a test!"
response.write LCase(strTest)
%>

结果:this is a test!

Left()

作用:返回字符串左边第length个字符以前的字符(含第length个字符).

语法:Left(string, length)

说明:

以下为引用的内容:
<%
strTest =   "This is a test!"
response.write Left(strTest, 3)
%>

结果:Thi

Len()

作用:返回字符串的长度.

语法:Len(string | varName)

说明:

以下为引用的内容:
<%
strTest =   "This is a test!"
response.write Len(strTest)
%>

结果:15

LTrim()
作用:去掉字符串左边的空格.

语法:LTrim(string)

说明:

以下为引用的内容:
<%
strTest =   " This is a test!"
response.write LTrim(strTest)
%>

结果:This is a test!

Mid()

作用:返回特定长度的字符串(从start开始,长度为length).

语法:Mid(string, start [, length >)

说明:

以下为引用的内容:
<%
strTest =   "This is a test! Today is Monday."
response.write Mid(strTest, 17, 5)
%>

结果:Today

Minute()

作用:返回时间的分钏.

语法:Minute(time)

说明:

<%=Minute(#12:45:32 PM#)%>

结果:45

Month()

作用:返回日期.

语法:Month(date)

说明:date is any valid date expression.

<%=Month(#08/04/99#)%>

结果:8

MonthName()

作用:Returns a string identifying the specified month.

语法:MonthName(month, [, Abb >)

说明:month is the numeric representation for a given month; Abb (optional) is a boolean value used to display month abbreviation. True will display the abbreviated month name and False (default) will not show the abbreviation.

<%=MonthName(Month(#08/04/99#))%>

结果:August

Now()

作用:Returns the current system date and time.返回当前系统时间

语法:Now()

说明:None

<%=Now%>

结果:8/4/99 9:30:16 AM

Replace()

作用:Returns a string in which a specified sub-string has been replaced with another substring a specified number of times.

语法:Replace(strToBeSearched, strSearchFor, strReplaceWith [, start [, count [, compare >>>)

以下为引用的内容:
说明:strToBeSearched is a string expression containing a sub-string to be replaced; strSearchFor is the string expression to search for within strToBeSearched; strReplaceWith is the string expression to replace sub-string strSearchFor; start (optional) is the numeric character position to begin search; count (optional) is a value indicating the comparision constant.  
<%
strTest =   "This is an apple!"
response.write Replace(strTest,   "apple", "orange")
%>

结果:This is an orange!

Right()

作用:返回字符串右边第length个字符以前的字符(含第length个字符).

语法:Right(string, length)

说明:

以下为引用的内容:
<%
strTest =   "This is an test!"
response.write Right(strTest, 3)
%>

结果:st!

Rnd()

作用:产生一个随机数.

语法:Rnd [ (number) >

说明:

以下为引用的内容:
<%
Randomize()
response.write RND()
%>

结果:任何一个在0 到 1 之间的数

Round()

作用:返回按指定位数进行四舍五入的数值.

语法:Round(expression [, numRight >)

说明:numRight数字表明小数点右边有多少位进行四舍五入。如果省略,则 Round 函数返回整数.

以下为引用的内容:
<%
i = 32.45678
response.write Round(i)
%>

结果:32

Rtrim()

作用:去掉字符串右边的空格字符串.

语法:Rtrim(string)

说明:

以下为引用的内容:
<%
strTest =   "This is a test!! "
response.write RTrim(strTest)
%>

结果:This is a test!!

Second()

作用:返回秒.

语法:Second(time)

说明:

<%=Second(#12:34:28 PM#)%>

结果:28

StrReverse()

作用:反排一字符串

语法:StrReverse(string)

说明

以下为引用的内容:
<%
strTest =   "This is a test!!"
response.write StrReverse(strTest)
%>

结果:!!tset a si sihT

Time()

作用:返回系统时间.

语法:Time()

说明:

<%=Time%>

结果:9:58:28 AM

Trim()

作用:去掉字符串左右的空格.

语法:Trim(string)

说明:string is any valid string expression.

以下为引用的内容:
<%
strTest =   " This is a test!! "
response.write Trim(strTest)
%>

结果:This is a test!!

UBound()

作用:返回指定数组维数的最大可用下标.

语法:Ubound(arrayname [, dimension >)

说明:dimension (optional) 指定返回哪一维上界的整数。1 表示第一维,2 表示第二维,以此类推。如果省略 dimension 参数,则默认值为 1.

以下为引用的内容:
<%
i = Array(  "Monday","Tuesday","Wednesday")
response.write UBound(i)
%>

结果:2

UCase()

作用:返回字符串的大写形式.

语法:UCase(string)

说明:

以下为引用的内容:
<%
strTest =   "This is a test!!"
response.write UCase(strTest)
%>

结果:THIS IS A TEST!!

VarType()

作用:返回指示变量子类型的值

语法:VarType(varName)

说明:

以下为引用的内容:
<%
i = 3
response.write varType(i)
%>

结果:2(数字)详见 "asp常数"

WeekDay()

作用:返回在一周的第几天.

语法:WeekDay(date [, firstdayofweek >)

说明:

以下为引用的内容:
<%
d = #8/4/99#
response.write Weekday(d)
%>

结果:4(星期三)

WeekDayName()

作用:返回一周第几天的名字.

语法:WeekDayName(weekday [, Abb [, firstdayofweek >>)

说明:Abb可选。Boolean 值,指明是否缩写表示星期各天的名称。如果省略, 默认值为 False,即不缩写星期各天的名称.firstdayofweek指明星期第一天的数值

以下为引用的内容:
<%
d = #8/4/99#
response.write WeekdayName(Weekday(d))
%>

结果:Wednesday

Year()

作用:返回当前的年份.

语法:Year(date)

说明:

<%=Year(#8/4/99#)%>

结果:1999

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: