VBS基础篇 - vbscript常用函数及功能 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
本文标签:vbs,常用函数 vbs常用函数
注释: Rnd 函数 语法:Rnd[(number)] 返回一随机数 。参数 number 可以是任何的数值表达式 。 注解: Rnd 函数返回的随机数介于 0 和 1 之间,可等于 0,但不等于 1 。 number 的值会影响 Rnd 返回的随机数:
各种转换函数及功能
注释: 1整型 2、长整型 3、单精度型(Single) 4、双精度型(Double) 常用的字符串函数及功能
常用的字符串函数及功能
常用日期和时间函数及说明
日期或时间间隔因子
常用的检验函数及功能
下面给大家一个常见实例,大家可以运行测试 Option Explicit '*********************************Date/Time函数******************************* 'CDate函数把一个合法的日期和事件表达式转换为Date类型,并返回结果 Dim d1 Dim d2 Dim d3 d1="April 22,2001" If IsDate(d1) Then MsgBox CDate(d1) End If d2=#2/22/01# If IsDate(d2) Then MsgBox CDate(d2) End If d3="3:18:40 AM" If IsDate(d3) Then MsgBox CDate(d3) End If 'Date函数返回当前系统的日期 '日期 MsgBox Date '日期+时间 MsgBox Now '时间 MsgBox Time 'DateAdd函数可返回已添加指定时间间隔的日期 MsgBox DateAdd("yyyy",1,"31-Jan-2003")'加一年 MsgBox DateAdd("yyyy",1,"31-Jan-2003")'减一年 MsgBox DateAdd("q",1,"31-Jan-2003")'加一个季度 MsgBox DateAdd("m",1,"31-Jan-2003")'加一个月 MsgBox DateAdd("y",1,"31-Jan-2003")'当年的第几天 MsgBox DateAdd("d",1,"31-Jan-2003")'增加一天 MsgBox DateAdd("w",1,"31-Jan-2003")'当周的第几天 MsgBox DateAdd("ww",1,"31-Jan-2003")'增加一个周 MsgBox DateAdd("h",1,"31-Jan-2003")'某日期的第一个小时 MsgBox DateAdd("n",1,"31-Jan-2003")'某日期的第一分钟 MsgBox DateAdd("s",1,"31-Jan-2003")'某日期的第一秒 'DateDiff函数可返回两个日期之间的时间间隔数 MsgBox DateDiff("yyyy",Date,"31-Jan-2012") MsgBox DateDiff("q",Date,"31-Jan-2012") MsgBox DateDiff("m",Date,"31-Jan-2012") MsgBox DateDiff("y",Date,"31-Jan-2012") MsgBox DateDiff("d",Date,"31-Jan-2012") MsgBox DateDiff("w",Date,"31-Jan-2012") MsgBox DateDiff("ww",Date,"31-Jan-2012") MsgBox DateDiff("h",Date,"31-Jan-2012") MsgBox DateDiff("n",Date,"31-Jan-2012") MsgBox DateDiff("s",Date,"31-Jan-2012") 'DatePart函数可返回给定日期的指定部分 Dim d d="2/10/2012 16:25:56" MsgBox DatePart("yyyy",d) MsgBox DatePart("m",d) MsgBox DatePart("q",d) MsgBox DatePart("w",d) MsgBox DatePart("ww",d) MsgBox DatePart("y",d) MsgBox DatePart("s",d) MsgBox DatePart("h",d) MsgBox DatePart("n",d) MsgBox DatePart("d",d) 'DateSerial函数可返回指定的年、月、日的子类型Date的Variant MsgBox DateSerial(2012,2,30) 'DateValue函数返回一个日期类型 MsgBox DateValue("31-Jan-2012 2:39:49 AM") 'Day函数可返回介于1到31之间的一个代表月的天数的数字 MsgBox Day(Date) 'FormatDateTime函数可格式化并返回一个额合法的日期或时间表达式 MsgBox FormatDateTime(Now,0) MsgBox FormatDateTime(Now,1) MsgBox FormatDateTime(Now,2) MsgBox FormatDateTime(Now,3) MsgBox FormatDateTime(Now,4) 'Hour函数可返回介于0到23之间的代表天的小时数的数字 MsgBox Hour(Now) 'IsDate函数可返回一个布尔值,指示经计算的表达式是否可被转换为日期,如果表达式是日期或可被转换为日期,则返回True,否则,返回False 。 MsgBox IsDate("15-3-2012") 'Minute函数可返回表示小时的分钟数的数字 MsgBox Minute(Now) 'Month函数可返回表示年的月份的数字 MsgBox Month(Now) 'Second函数可返回表示分钟的秒数的数字 MsgBox Second(Now) 'Time函数可返回当前的系统时间 MsgBox Time 'Timer函数可返回午夜12时以后已经过去的秒数 MsgBox Timer 'TimeSerial函数可把时、分、秒合并成为时间 MsgBox TimeSerial(3,45,50) 'TimeValue函数可返回包含时间的日期子类型的变量 MsgBox TimeValue(Now) 'Weekday函数可返回表示一周的天数的数字,介于1和7之间 。 MsgBox Weekday(Date,0) 'WeekdayName函数可返回一周中指定一天的星期名 MsgBox WeekdayName(3) MsgBox WeekdayName(Weekday(Date,1)) 'Year函数可返回表示年份的一个数字 MsgBox Year(date) '*********************************Conversion函数******************************* 'Asc函数可把字符串中的第一个字母转换为对应的ANSI代码,并返回结果 MsgBox Asc("A") MsgBox Asc("a") 'CBool函数可把表达式转换为布尔类型 Dim a a=5 MsgBox CBool(a) 'CByte函数可把表达式转换为字节类型 Dim b b=134.12345 MsgBox CByte(b) 'CCur函数可把表达式转换为货币类型 Dim c c=12345.12345656 MsgBox CCur(c) 'CDbl函数可把表达式转换为双精度类型 Dim e e=123.12454656577435235436 MsgBox CDbl(e) 'Chr函数可把指定的ANSI字符代码转换为字符 MsgBox Chr(65) 'CInt函数可把表达式转换为整数类型 MsgBox CInt(12.3435) 'CLng函数可把表达式转换为长整形类型 MsgBox CLng(12.676) MsgBox CLng(12.243) 'CSng函数可把表达式转换为单精度类型 MsgBox CSng(122.355465) MsgBox CSng(21.23222) 'CStr函数可把表达式转换为字符串类型 MsgBox CStr(false) MsgBox CStr(Date) MsgBox CStr(Empty) MsgBox CStr(Error) '*********************************Format函数******************************* 'FormatCurrency函数可返回作为货币值被格式化的表达式,使用系统控制面板中定义的货币符号 MsgBox FormatCurrency(21.23456) 'FormatNumber函数可返回作为数字被格式化的表达式 MsgBox FormatNumber(12.23456775,3) 'FormatPercent函数可返回作为百分数被格式化的表达式 MsgBox FormatPercent(3/7,3) '*********************************Math函数******************************* 'Abs函数可返回指定的数字的绝对值 MsgBox Abs(-9) 'Atn函数可返回指定数字的正切 MsgBox Atn(9) 'Cos函数可返回指定数字的余弦 MsgBox Cos(9) 'Exp函数可e的幂次方 MsgBox Exp(2.3) 'Fix函数可返回指定数字的整数部分 MsgBox Fix(-6.325) MsgBox Int(-6.325) 'Log函数可返回指定数据的自然对数 MsgBox Log(10) 'Rnd函数可返回一个随机数,数字总是小于1但大于或等于0. MsgBox Rnd 'Sgn函数可返回指定数字的符号的整数 MsgBox Sgn(15) MsgBox Sgn(-5) MsgBox Sgn(0) 'Sin函数可返回指定数字的正弦 MsgBox Sin(9) 'Sqr函数可返回一个数的平方根 MsgBox Sqr(9) 'Tan函数可返回指定数字的正切 MsgBox Tan(9) '*********************************Array函数******************************* 'Array可返回一个包含数组的变量 Dim Arr Arr=Array(1,2,3,4,5) MsgBox Arr(0) 'Filter函数可返回一个基于0的数组,此数组包含以特定过滤条件为基础的字符串数组的子集 Dim Arr1(5),b Arr1(0)="Saturday" Arr1(1)="Sunday" Arr1(2)="Monday" Arr1(3)="Tuesday" Arr1(4)="Wednesday" b=Filter(Arr1,"n",True) MsgBox b(0) MsgBox b(1) MsgBox b(2) 'ISArray函数可返回一个指示指定的变量是否为数组的布尔值 。如果变量为数组,则返回True,否则返回False 。 Dim a(3) a(0)=1 a(1)=2 a(2)=3 MsgBox IsArray(a) 'Join函数可返回一个由某个数组中一系列子字符串组成的字符串 Dim a(5),b a(0)="Saturday" a(1)="Sunday" a(2)="Monday" a(3)="Wendesday" a(4)="Tuesday" b=Filter(a,"n",True) MsgBox Join(b,",") 'LBound函数可返回指示数组维数的最小下标 。(始终为0) 'UBound函数可返回指示数组维数的最大下标 。 Dim a(5) MsgBox LBound(a) MsgBox UBound(a) 'Split函数可返回基于0的一维数组,此数组包含指定的子字符串 Dim txt,a txt="hello/world!" a=Split(txt,"/") MsgBox a(0) MsgBox a(1) '*********************************String函数******************************* 'InStr函数可返回一个字符串在另一个字符串中首次出现的位置 Dim txt,pos txt="This is a beautiful day!" pos=InStr(4,txt,"is",1) MsgBox pos 'InStrRev函数可返回一个字符串在另一个字符串中首次出现的位置,搜索从字符串的末端开始,但是返回的位置是从字符串的起点开始计数的 。 Dim txt,pos txt="This is a beautiful day!" pos=InStrRev(txt,"i",-1,1) MsgBox pos 'LCase函数可把指定的字符串转换为小写 'UCase函数可把指定的字符串转换为大写 Dim txt,pos1,pos2 txt="This Is A Beautiful Day!" pos1=LCase(txt) pos2=UCase(txt) MsgBox pos1 MsgBox pos2 'Left函数可从字符串的左侧返回指定数目的字符 'Right函数可从字符串的右侧返回指定数目的字符 Dim txt,pos1,pos2 txt="This is a beautiful day!" pos1=Left(txt,5) pos2=Right(txt,6) MsgBox pos1 MsgBox pos2 'Len函数可返回字符串中字符的数目 Dim txt,pos txt="This is a beautiful day!" pos=Len(txt) MsgBox pos 'LTrim函数可删除字符串左侧的空格 'RTrim函数可删除字符串右侧的空格 'Trim函数可删除字符串两端的空格 Dim txt,pos1,pos2,pos3 txt=" This is a beautiful day! " pos1=LTrim(txt) pos2=RTrim(txt) pos3=Trim(txt) MsgBox pos1 MsgBox pos2 MsgBox pos3 MsgBox Len(pos1) MsgBox Len(pos2) MsgBox Len(pos3) 'Mid函数可从字符串中返回指定数目的字符 Dim txt txt="This is a beautiful day!" MsgBox Mid(txt,1,5) MsgBox Mid(txt,2,5) MsgBox Mid(txt,1) MsgBox Mid(txt,5) 'Replace函数可使用一个字符串替换另一个字符串指定的次数 Dim txt txt="This is a beautiful day!" MsgBox Replace(txt,"is","AA",4,2) 'Space函数可返回一个由指定数目的空格组成的字符串 Dim txt txt=Space(20) MsgBox txt MsgBox Len(txt) 'StrComp函数可比较两个字符串,并返回表示比较结果的一个值 Dim txt1,txt2 txt1="hello" txt2="hell" MsgBox StrComp(txt1,txt2) 'String函数可返回包含指定长度的重复字符的一个字符串 MsgBox String(10,"*") 'StrReverse函数可反转一个字符串 Dim txt txt="This is a beautiful day!" MsgBox StrReverse(txt) '*********************************其他函数******************************* 'InputBox函数可显示一个对话框,用户可在其中输入文本或点击一个按钮 。 Dim fname fname=InputBox("Enter your name:") MsgBox ("Your name is "&fname) 'IsEmpty函数可返回指定的变量是否被初始化的布尔值 。 Dim x,y y=10 MsgBox IsEmpty(x) MsgBox IsEmpty(y) 'IsNull函数可返回指定表达式是否无效数据的布尔值 Dim x,y,z x=Null y=10 z="" MsgBox IsNull(x) MsgBox IsNull(y) MsgBox IsNull(z) 'IsNumeric函数可返回指示指定的表达式是否可作为数字来计算的布尔值 。 Dim x x=10 MsgBox IsNumeric(x) x=Empty MsgBox IsNumeric(x) x="1 h" MsgBox IsNumeric(x) 这篇关于vbscript常见函数的文章就介绍到这里,希望大家以后多多支持脚本之家 。 |