vbs常用函数
函数名 | 语法 | 功能 | Abs | Abs(number) | 返回一个数的绝对值 | Sqr | Sqr(number) | 返回一个数的平方根 | Sin | Sin(number) | 返回角度的正玄值 | Cos | Cos(number) | 返回角度的余玄值 | Tan | Tan(number) | 返回角度的正切值 | Atn | Atn(number) | 返回角度的反正切值 | Log | Log(number) | 返回一个数的自然对数 | Int | Int(number) | 取整函数,返回一个小于number的第一整数 | FormatNumber | FormatNumber(number, numdigitsafterdecimal) | 转化为指定小数位数(numdigitsafterdecimal)的数字 | Rnd | Rnd() | 返回一个从0到1的随机数 | Ubound | Ubound(数组名,维数) | 返回该数组的最大下标 | Lbound | Lbound(数组名,维数) | 返回最小下标数 |
注释:
Rnd 函数
语法:Rnd[(number)]
返回一随机数。参数 number 可以是任何的数值表达式。
注解:
Rnd 函数返回的随机数介于 0 和 1 之间,可等于 0,但不等于 1。
number 的值会影响 Rnd 返回的随机数:
Number的取值 | 返回值 | 小于0 | 每次都是使用numbe当做随机结果。 | 大于0 | 随机序列中的下一个随机数。 | 等于0 | 最近一次产生过的随机数。 | 省略 | 随机序列中的下一个随机数。 |
各种转换函数及功能
函数 | 功能 | CStr(variant) | 将变量variant转化为字符串类型 | CDate(variant) | 将变量variant转化为日期类型 | CInt(variant) | 将变量variant转化为整数类型 | CLng(variant) | 将变量variant转化为长整数类型 | CSng(variant) | 将变量variant转化为single类型 | CDbl(variant) | 将变量variant转化为double类型 | CBool(variant) | 将变量variant转化为布尔类型 |
注释:
1整型
以Integer 表示整型,其范围为 -32,768 到 32,767 之间。
2、长整型
Long(长整型) ,其范围从 -2,147,483,648 到 2,147,483,647。
3、单精度型(Single)
Single(单精度浮点型),它的范围在负数的时候是从 -3.402823E38 到 -1.401298E-45,而在正数的时候是从 1.401298E-45 到 3.402823E38。
4、双精度型(Double)
Double(双精度浮点型)它的范围在负数的时候是从 -1.79769313486232E308 到 -4.94065645841247E-324,而正数的时候是从 4.94065645841247E-324 到 1.79769313486232E308。
常用的字符串函数及功能
函数 | 语法 | 功能 | Len | Len(string) | 返回string字符串里的字符数目 | Trim | Trim(string) | 将字符串前后的空格去掉 | Ltrim | Ltrim(string) | 将字符串前面的空格去掉 | Rtrim | Rtrim(string) | 将字符串后面的空格去掉 | Mid | Mid(string,start,length) | 从string字符串的start字符开始取得length长度的字符串,如果省略第三个参数表示从start字符开始到字符串结尾的字符串 | Left | Left(string,length) | 从string字符串的左边取length长度的字符串 | Right | Right(string,length) | 从srting字符串的右边取得length长度的字符串 | LCase | LCase(string) | 将字符串里的所有大写字母转化成小写字母 | UCase | UCase(string) | 将字符串里的小写字母转化成大写字母 | StrComp | Strcomp(string1,string1) | 返回string1字符串与string2字符串的比较结果,如果两个字符串相同,返回0 | InStr | InStr(string1,string2) | 返回string2字符串在string1字符串中第一次出现的位置 | Split | Split(string1,delimiter | 将字符串根据delimiter拆分成一维数组,其中delimiter用于表示子字符串界限的字符,如果省略,使用空格(“”)当作分隔符 | Replace | Replace(string1,find,replacewith) | 返回字符串,其中指定的子字符串(find)被替换为另一个子字符串(replacewith) |
常用的字符串函数及功能
函数 | 语法 | 功能 | Len | Len(string) | 返回string字符串里的字符数目 | Trim | Trim(string) | 将字符串前后的空格去掉 | Ltrim | Ltrim(string) | 将字符串前面的空格去掉 | Rtrim | Rtrim(string) | 将字符串后面的空格去掉 | Mid | Mid(string,start,length) | 从string字符串的start字符开始取得length长度的字符串,如果省略第三个参数表示从start字符开始到字符串结尾的字符串 | Left | Left(string,length) | 从string字符串的左边取length长度的字符串 | Right | Right(string,length) | 从srting字符串的右边取得length长度的字符串 | LCase | LCase(string) | 将字符串里的所有大写字母转化成小写字母 | UCase | UCase(string) | 将字符串里的小写字母转化成大写字母 | StrComp | Strcomp(string1,string1) | 返回string1字符串与string2字符串的比较结果,如果两个字符串相同,返回0 | InStr | InStr(string1,string2) | 返回string2字符串在string1字符串中第一次出现的位置 | Split | Split(string1,delimiter | 将字符串根据delimiter拆分成一维数组,其中delimiter用于表示子字符串界限的字符,如果省略,使用空格(“”)当作分隔符 | Replace | Replace(string1,find,replacewith) | 返回字符串,其中指定的子字符串(find)被替换为另一个子字符串(replacewith) |
常用日期和时间函数及说明
函数 | 语法 | 功能 | Now | Now() | 取得系统当前的日期和时间 | Date | Date() | 取得系统当前的日期 | Time | Time() | 取得系统当前的时间 | Year | Year() | 取得给定日期的年份 | Month | Month(Date) | 取得给定日期的月份 | Day | Day(Date) | 取得给定日期是几号 | Hour | Hour(time) | 取得给定时间是第几小时 | Minute | Minute(time) | 取得给定时间是第几分钟 | Second | Second(time) | 取得给定时间是第几秒 | WeekDay | WeekDay(Date) | 取得给定日期是星期几的整数
1 表示星期一
2 表示星期二
依次类推 | DateDiff | DateDiff(“Var”, Var1,Var2)
Var:日期或时间间隔因子
Var1:第一个日期或时间
Var2:第二个日期或时间 | 计算两个日期或时间的间隔 | DateAdd | DateAdd(“Var”, Var1,Var2)
Var:日期或时间间隔因子
Var1:日期或时间
Var2:日期或时间 | 对两个日期或时间作加法
DateAdd(“d”,10,Date()) 10天后是几号 | FormatDateTime | FortDateTime(Date,vbShortDate) | 转化为短日期格式 | FortDateTime(Date,vblongDate) | 转化为长日期格式 | FortDateTime(Date,vbShortTime) | 转化为短时间格式 | FortDateTime(Date,vbLongTime) | 转化为长时间格式 |
日期或时间间隔因子
常用的检验函数及功能
函数 | 功能 | VarType(variant) | 检查变量vriant的值,函数值为该变量的数据子类型,0表示空,2表示整数,7表示日子,8表示字符串,11表示布尔变量,8192表示数组 | IsNumeric(variant) | 检查变量variant的值,如果variant是数值类型,则函数值为ture | IsNull(variant) | 检查变量variant的值,如果variant为null,则函数值为ture | IsEmpty(variant) | 检查变量的值,如果variant是empty,则函数值为ture | IsObject(variant) | 检查变量variant的值,如果variant是对象类型,则函数值为ture | IsDate(variant) | 检查变量variant的值,如果variant是日期类型,则函数值为ture | IsArray(variant) | 检查变量variant的值,如果variant是数组类型,则函数值为ture |
下面给大家一个常见实例,大家可以运行测试- 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常见函数的文章就介绍到这里,希望大家以后多多支持脚本之家。
来源:互联网
免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作! |
|