通过实例讲解来学习ASP中的函数
<H2>Array()</H2><P>作用:返回一个数组<BR>语法:Array(list)<BR>适用的类型:字符,数字均可<BR></P><PRE class=code><%
Dim myArray()
For i = 1 to 7
Redim Preserve myArray(i)
myArray(i) = WeekdayName(i)
Next
%>
</PRE>
<P>结果:建立了一个包含7个元素的数组myArray<BR>myArray("Sunday","Monday", ... ... "Saturday") </P>
<H2>CInt()</H2>
<P>作用:将一个表达式转化为数字类型<BR>语法:CInt(表达式)<BR>适用的类型:任何有效的字符均可<BR></P><PRE class=code><%
f = "234"
response.write cINT(f) + 2
%>
</PRE>
<P>结果:236<BR>转化字符"234"为数字234,如果字符串为空,则返回0值 </P>
<H2>CreateObject()</H2>
<P>作用: 建立和返回一个已注册的ACTIVEX组件的实例。<BR> 语法: CreateObject(objName) <BR>适用的类型: objName 是任何一个有效、已注册的ACTIVEX组件的名字. <BR></P><PRE class=code><%
Set con = Server.CreateObject("ADODB.Connection")
%></PRE>
<H2>CStr()</H2>
<P>作用: 转化一个表达式为字符串.<BR>语法: CStr(expression) <BR>适用类型: expression 是任何有效的表达式 </P><PRE class=code><%
s = 3 + 2
response.write("The 结果 is: " & cStr(s))
%>
</PRE>
<P>结果: 转化数字5为字符“5”。</P>
<H2>Date()</H2>
<P>作用: 返回当前系统日期.<BR>语法: Date() <BR>适用的类型: None. </P><PRE class=code><%=Date%>
</PRE>
<P>结果: 8/4/99 </P>
<H2>DateAdd()</H2>
<P>作用: 返回一个被改变了的日期。<BR>语法: DateAdd(timeinterval,number,date) <BR>说明: timeinterval为所要加入的时间间隔类型; number 为要添加的数量; date 为起始日期. </P><PRE class=code><%
currentDate = #8/4/99#
newDate = DateAdd( "m",3,currentDate)
response.write newDate
%>
</PRE><PRE class=code><%
currentDate = #12:34:45 PM#
newDate = DateAdd( "h",3,currentDate)
response.write newDate
%>
</PRE>
<P>结果: 11/4/99 <BR>3:34:45 PM </P>
<P>"m" = "month";<BR> "d" = "day";</P>
<P>当当前日期格式为time,那么<BR>"h" = "hour";<BR>"s" = "second";</P>
<H2>DateDiff()</H2>
<P>作用: 返回两个日期之间的差值 。<BR>语法: DateDiff(timeinterval,date1,date2 [, firstdayofweek [, firstweekofyear >>)<BR>说明: timeinterval 表示相隔时间的类型,如“M“表示“月”。 </P><PRE class=code><%
fromDate = #8/4/99#
toDate = #1/1/2000#
response.write("There are " & _
DateDiff("d",fromDate,toDate) & _
" days to millenium from 8/4/99."
%>
</PRE>
<P>结果: There are150daysto millenium from 8/4/99.</P>
<H2>Day()</H2>
<P>作用: 返回一个月的第几日 .<BR>语法: Day(date) <BR>说明: date 是任何有效的日期。 </P><PRE class=code><%=Day(#8/4/99#)%>
</PRE>
<P>结果: 4 </P>
<H2>FormatCurrency()</H2>
<P>作用: 返回表达式,此表达式已被格式化为货币值<BR>语法: FormatCurrency(Expression [, Digit [, LeadingDigit [, Paren [, GroupDigit >>>>)<BR>说明: Digit 指示小数点右侧显示位数的数值。默认值为 -1,指示使用的是计算机的区域设置; LeadingDigit 三态常数,指示是否显示小数值小数点前面的零 </P><PRE class=code><%=FormatCurrency(34.3456)%>
</PRE>
<P>结果: $34.35 </P>
<H2>FormatDateTime()</H2>
<P>作用: 返回表达式,此表达式已被格式化为日期或时间<BR>语法: FormatDateTime(Date, [, NamedFormat >)<BR>说明: NamedFormat 指示所使用的日期/时间格式的数值,如果省略,则使用 vbGeneralDate. </P><PRE class=code><%=FormatDateTime("08/4/99", vbLongDate)%>
</PRE>
<P>结果: Wednesday, August 04, 1999 </P>
<H2>FormatNumber()</H2>
<P>作用: 返回表达式,此表达式已被格式化为数值.<BR>语法: FormatNumber(Expression [, Digit [, LeadingDigit [, Paren [, GroupDigit >>>>)<BR>说明: Digit 指示小数点右侧显示位数的数值。默认值为 -1,指示使用的是计算机的区域设置。; LeadingDigit i指示小数点右侧显示位数的数值。默认值为 -1,指示使用的是计算机的区域设置。; Paren 指示小数点右侧显示位数的数值。默认值为 -1,指示使用的是计算机的区域设置。; GroupDigit i指示小数点右侧显示位数的数值。默认值为 -1,指示使用的是计算机的区域设置 </P><PRE class=code><%=FormatNumber(45.324567, 3)%>
</PRE>
<P>结果: 45.325 </P>
<H2>FormatPercent()</H2>
<P>作用: 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 (%)<BR>语法: FormatPercent(Expression [, Digit [, LeadingDigit [, Paren [, GroupDigit >>>>)<BR>说明: 同上. </P><PRE class=code><%=FormatPercent(0.45267, 3)%>
</PRE>
<P>结果: 45.267% </P>
<H2>Hour()</H2>
<P>作用: 以24时返回小时数.<BR>语法: Hour(time) <BR>说明: </P><PRE class=code><%=Hour(#4:45:34 PM#)%></PRE>
<P>结果: 16 </P>
<H2>Instr()</H2>
<P>作用: 返回字符或字符串在另一个字符串中第一次出现的位置.<BR>语法: Instr([start, > strToBeSearched, strSearchFor [, compare>)<BR>说明: Start为搜索的起始值,strToBeSearched接受搜索的字符串 strSearchFor要搜索的字符compare 比较方式(详细见ASP常数) </P><PRE class=code><%
strText = "This is a test!!"
pos = Instr(strText, "a")
response.write pos
%>
</PRE>
<P>结果: 9 </P>
<H2>InstrRev()</H2>
<P>作用: 同上,只是从字符串的最后一个搜索起<BR>语法: InstrRev([start, > strToBeSearched, strSearchFor [, compare>)<BR>说明: 同上.</P><PRE class=code><%
strText = "This is a test!!"
pos = InstrRev(strText, "s")
response.write pos
%>
</PRE>
<P>结果: 13 </P>
<H2>Int()</H2>
<P> 作用: 返回数值类型,不四舍五入。 <BR>语法: Int(number) <BR>说明: </P><PRE class=code><%=INT(32.89)%>
</PRE>
<P>结果: 32 </P>
<H2>IsArray()</H2>
<P>作用: 判断一对象是否为数组,返回布尔值 .<BR>语法: IsArray(name) <BR>说明: </P><PRE class=code><%
strTest = "Test!"
response.write IsArray(strTest)
%>
</PRE>
<P> 结果: False </P>
<H2>IsDate()</H2>
<P>作用: 判断一对象是否为日期,返回布尔值语法: IsDate(expression) 说明: expression is any valid expression. </P><PRE class=code><%
strTest = "8/4/99"
response.write IsDate(strTest)
%>
</PRE>
<P> 结果: True </P>
<H2>IsEmpty()</H2>
<P>作用: 判断一对象是否初始化,返回布尔值.<BR>语法: IsEmpty(expression) <BR>说明: </P><PRE class=code><%
Dim i
response.write IsEmpty(i)
%>
</PRE>
<P>结果: True </P>
<H2>IsNull()</H2>
<P> 作用: 判断一对象是否为空,返回布尔值.<BR> 语法: IsNull(expression) <BR> 说明: <BR><%<BR>Dim i<BR>response.write IsNull(i)<BR>%></P>
<P>结果: False </P>
<H2>IsNumeric()</H2>
<P>作用: 判断一对象是否为数字,返回布尔值. <BR> 语法: IsNumeric(expression) <BR> 说明: <BR><%<BR>i = "345"<BR>response.write IsNumeric(i)<BR>%></P>
<P>结果: True <BR> 就算数字加了引号,ASP还是认为它是数字。 </P>
<H2>IsObject()</H2>
<P>作用: 判断一对象是否为对象,返回布尔值.<BR> 语法: IsObject(expression) <BR> 说明:</P><PRE class=code><%
Set con = Server.CreateObject( "ADODB.Connection")
response.write IsObject(con)
%>
</PRE>
<P>结果: True </P>
<H2>LBound()</H2>
<P>作用: 返回指定数组维的最小可用下标. <BR> 语法: Lbound(arrayname [, dimension >)<BR> 说明: dimension 指明要返回哪一维下界的整数。使用 1 表示第一维,2 表示第二维,以此类 推。如果省略 dimension 参数,默认值为 1. </P><PRE class=code><%
i = Array( "Monday","Tuesday","Wednesday")
response.write LBound(i)
%></PRE>
<P>结果: 0 </P>
<H2>LCase()</H2>
<P>作用: 返回字符串的小写形式<BR> 语法: Lcase(string) <BR> 说明: string is any valid string expression. </P><PRE class=code><%
strTest = "This is a test!"
response.write LCase(strTest)
%>
</PRE>
<P>结果: this is a test! </P>
<H2>Left()</H2>
<P>作用: 返回字符串左边第length个字符以前的字符(含第length个字符).<BR> 语法: Left(string, length) <BR> 说明:</P><PRE class=code><%
strTest = "This is a test!"
response.write Left(strTest, 3)
%>
</PRE>
<P>结果: Thi </P>
<H2>Len()</H2>
<P>作用: 返回字符串的长度.<BR> 语法: Len(string | varName) <BR> 说明: </P><PRE class=code><%
strTest = "This is a test!"
response.write Len(strTest)
%>
</PRE>
<P>结果: 15 </P>
<H2>LTrim()</H2>
<P>作用: 去掉字符串左边的空格.<BR> 语法: LTrim(string) <BR> 说明: </P><PRE class=code><%
strTest = " This is a test!"
response.write LTrim(strTest)
%>
</PRE>
<P>结果: This is a test! </P>
<H2>Mid()</H2>
<P>作用: 返回特定长度的字符串(从start开始,长度为length).<BR> 语法: Mid(string, start [, length >)<BR> 说明: </P><PRE class=code><%
strTest = "This is a test! Today is Monday."
response.write Mid(strTest, 17, 5)
%>
</PRE>
<P>结果: Today </P>
<H2>Minute()</H2>
<P>作用: 返回时间的分钏.<BR> 语法: Minute(time) <BR> 说明:</P><PRE class=code><%=Minute(#12:45:32 PM#)%></PRE>
<P>结果: 45 </P>
<H2>Month()</H2>
<P>作用: 返回日期.<BR> 语法: Month(date) <BR> 说明: date is any valid date expression.</P><PRE class=code><%=Month(#08/04/99#)%></PRE>
<P>结果: 8 </P>
<H2>MonthName()</H2>
<P>作用: Returns a string identifying the specified month.<BR> 语法: MonthName(month, [, Abb >)<BR> 说明: 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. </P><PRE class=code><%=MonthName(Month(#08/04/99#))%></PRE>
<P>结果: August </P>
<H2>Now()</H2>
<P>作用: Returns the current system date and time.返回当前系统时间<BR> 语法: Now() <BR> 说明: None </P><PRE class=code><%=Now%></PRE>
<P>结果: 8/4/99 9:30:16 AM </P>
<H2>Replace()</H2>
<P>作用: Returns a string in which a specified sub-string has been replaced with another substring a specified number of times.<BR> 语法: Replace(strToBeSearched, strSearchFor, strReplaceWith [, start [, count [, compare >>>)<BR> 说明: 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. <BR><%<BR>strTest = "This is an apple!"<BR>response.write Replace(strTest, "apple", "orange")<BR>%><BR></P>
<P>结果: This is an orange! </P>
<H2>Right()</H2>
<P>作用: 返回字符串右边第length个字符以前的字符(含第length个字符).<BR> 语法: Right(string, length) <BR> 说明: . </P><PRE class=code><%
strTest = "This is an test!"
response.write Right(strTest, 3)
%>
</PRE>
<P>结果: st! </P>
<H2>Rnd()</H2>
<P>作用: 产生一个随机数.<BR> 语法: Rnd [ (number) ><BR> 说明: </P><PRE class=code><%
Randomize()
response.write RND()
%>
</PRE>
<P>结果: 任何一个在0 到 1 之间的数 </P>
<H2>Round()</H2>
<P>作用: 返回按指定位数进行四舍五入的数值.<BR> 语法: Round(expression [, numRight >)<BR> 说明: numRight数字表明小数点右边有多少位进行四舍五入。如果省略,则 Round 函数返回整数. </P><PRE class=code><%
i = 32.45678
response.write Round(i)
%>
</PRE>
<P>结果: 32 </P>
<H2>Rtrim()</H2>
<P>作用: 去掉字符串右边的空格字符串.<BR> 语法: Rtrim(string) <BR> 说明: </P><PRE class=code><%
strTest = "This is a test!! "
response.write RTrim(strTest)
%>
</PRE>
<P> 结果: This is a test!! </P>
<H2>Second()</H2>
<P>作用: 返回秒.<BR> 语法: Second(time) <BR> 说明: </P><PRE class=code><%=Second(#12:34:28 PM#)%></PRE>
<P>结果: 28 </P>
<H2>StrReverse()</H2>
<P>作用: 反排一字符串<BR> 语法: StrReverse(string) <BR> 说明: </P><PRE class=code><%
strTest = "This is a test!!"
response.write StrReverse(strTest)
%></PRE> 结果: !!tset a si sihT
<H2>Time()</H2>
<P>作用: 返回系统时间.<BR> 语法: Time() <BR> 说明: . </P><PRE class=code><%=Time%></PRE>
<P>结果: 9:58:28 AM</P>
<H2>Trim()</H2>
<P>作用: 去掉字符串左右的空格.<BR> 语法: Trim(string) <BR> 说明: string is any valid string expression.</P><PRE class=code><%
strTest = " This is a test!! "
response.write Trim(strTest)
%>
</PRE>
<P> 结果: This is a test!! </P>
<H2>UBound()</H2>
<P>作用: 返回指定数组维数的最大可用下标.<BR> 语法: Ubound(arrayname [, dimension >)<BR> 说明: dimension (optional) 指定返回哪一维上界的整数。1 表示第一维,2 表示第二维,以此类推。如果省略 dimension 参数,则默认值为 1. </P><PRE class=code><%
i = Array( "Monday","Tuesday","Wednesday")
response.write UBound(i)
%>
</PRE>
<P>结果: 2 </P>
<H2>UCase()</H2>
<P>作用: 返回字符串的大写形式.<BR> 语法: UCase(string) <BR> 说明: </P><PRE class=code><%
strTest = "This is a test!!"
response.write UCase(strTest)
%>
</PRE>
<P> 结果: THIS IS A TEST!! </P>
<H2>VarType()</H2>
<P>作用: 返回指示变量子类型的值<BR> 语法: VarType(varName) <BR> 说明: </P><PRE class=code><%
i = 3
response.write varType(i)
%>
</PRE>
<P>结果: 2(数字)详见 "asp常数"</P>
<H2>WeekDay()</H2>
<P>作用: 返回在一周的第几天.<BR> 语法: WeekDay(date [, firstdayofweek >)<BR> 说明: .</P><PRE class=code><%
d = #8/4/99#
response.write Weekday(d)
%>
</PRE>
<P> 结果: 4(星期三) </P>
<H2>WeekDayName()</H2>
<P>作用: 返回一周第几天的名字.<BR> 语法: WeekDayName(weekday [, Abb [, firstdayofweek >>)<BR> 说明: Abb可选。Boolean 值,指明是否缩写表示星期各天的名称。如果省略, 默认值为 False,即不缩写星期各天的名称.firstdayofweek指明星期第一天的数值 </P><PRE class=code><%
d = #8/4/99#
response.write WeekdayName(Weekday(d))
%>
</PRE>
<P>结果: Wednesday </P>
<H2>Year()</H2>
<P>作用: 返回当前的年份.<BR> 语法: Year(date) <BR> 说明:</P><PRE class=code><%=Year(#8/4/99#)%></PRE>
<P>结果: 1999</P> 不错哦,我来支持下 强烈支持啊! 好好学习天天向上啊!
页:
[1]