函數(shù)名 | 語法 | 功能 |
Abs | Abs(number) | 返回一個數(shù)的絕對值 |
Sqr | Sqr(number) | 返回一個數(shù)的平方根 |
Sin | Sin(number) | 返回角度的正玄值 |
Cos | Cos(number) | 返回角度的余玄值 |
Tan | Tan(number) | 返回角度的正切值 |
Atn | Atn(number) | 返回角度的反正切值 |
Log | Log(number) | 返回一個數(shù)的自然對數(shù) |
Int | Int(number) | 取整函數(shù),返回一個小于number的第一整數(shù) |
FormatNumber | FormatNumber(number, numdigitsafterdecimal) | 轉(zhuǎn)化為指定小數(shù)位數(shù)(numdigitsafterdecimal)的數(shù)字 |
Rnd | Rnd() | 返回一個從0到1的隨機數(shù) |
Ubound | Ubound(數(shù)組名,維數(shù)) | 返回該數(shù)組的最大下標 |
Lbound | Lbound(數(shù)組名,維數(shù)) | 返回最小下標數(shù) |
注釋:
Rnd 函數(shù)
語法:Rnd[(number)]
返回一隨機數(shù)。參數(shù) number 可以是任何的數(shù)值表達式。
注解:
Rnd 函數(shù)返回的隨機數(shù)介于 0 和 1 之間,可等于 0,但不等于 1。
number 的值會影響 Rnd 返回的隨機數(shù):
Number的取值 | 返回值 |
小于0 | 每次都是使用numbe當做隨機結(jié)果。 |
大于0 | 隨機序列中的下一個隨機數(shù)。 |
等于0 | 最近一次產(chǎn)生過的隨機數(shù)。 |
省略 | 隨機序列中的下一個隨機數(shù)。 |
各種轉(zhuǎn)換函數(shù)及功能
函數(shù) | 功能 |
CStr(variant) | 將變量variant轉(zhuǎn)化為字符串類型 |
CDate(variant) | 將變量variant轉(zhuǎn)化為日期類型 |
CInt(variant) | 將變量variant轉(zhuǎn)化為整數(shù)類型 |
CLng(variant) | 將變量variant轉(zhuǎn)化為長整數(shù)類型 |
CSng(variant) | 將變量variant轉(zhuǎn)化為single類型 |
CDbl(variant) | 將變量variant轉(zhuǎn)化為double類型 |
CBool(variant) | 將變量variant轉(zhuǎn)化為布爾類型 |
注釋:
1整型
以Integer 表示整型,其范圍為 -32,768 到 32,767 之間。
2、長整型
Long(長整型) ,其范圍從 -2,147,483,648 到 2,147,483,647。
3、單精度型(Single)
Single(單精度浮點型),它的范圍在負數(shù)的時候是從 -3.402823E38 到 -1.401298E-45,而在正數(shù)的時候是從 1.401298E-45 到 3.402823E38。
4、雙精度型(Double)
Double(雙精度浮點型)它的范圍在負數(shù)的時候是從 -1.79769313486232E308 到 -4.94065645841247E-324,而正數(shù)的時候是從 4.94065645841247E-324 到 1.79769313486232E308。
常用的字符串函數(shù)及功能
函數(shù) | 語法 | 功能 |
Len | Len(string) | 返回string字符串里的字符數(shù)目 |
Trim | Trim(string) | 將字符串前后的空格去掉 |
Ltrim | Ltrim(string) | 將字符串前面的空格去掉 |
Rtrim | Rtrim(string) | 將字符串后面的空格去掉 |
Mid | Mid(string,start,length) | 從string字符串的start字符開始取得length長度的字符串,如果省略第三個參數(shù)表示從start字符開始到字符串結(jié)尾的字符串 |
Left | Left(string,length) | 從string字符串的左邊取length長度的字符串 |
Right | Right(string,length) | 從srting字符串的右邊取得length長度的字符串 |
LCase | LCase(string) | 將字符串里的所有大寫字母轉(zhuǎn)化成小寫字母 |
UCase | UCase(string) | 將字符串里的小寫字母轉(zhuǎn)化成大寫字母 |
StrComp | Strcomp(string1,string1) | 返回string1字符串與string2字符串的比較結(jié)果,如果兩個字符串相同,返回0 |
InStr | InStr(string1,string2) | 返回string2字符串在string1字符串中第一次出現(xiàn)的位置 |
Split | Split(string1,delimiter | 將字符串根據(jù)delimiter拆分成一維數(shù)組,其中delimiter用于表示子字符串界限的字符,如果省略,使用空格(“”)當作分隔符 |
Replace | Replace(string1,find,replacewith) | 返回字符串,其中指定的子字符串(find)被替換為另一個子字符串(replacewith) |
常用的字符串函數(shù)及功能
函數(shù) | 語法 | 功能 |
Len | Len(string) | 返回string字符串里的字符數(shù)目 |
Trim | Trim(string) | 將字符串前后的空格去掉 |
Ltrim | Ltrim(string) | 將字符串前面的空格去掉 |
Rtrim | Rtrim(string) | 將字符串后面的空格去掉 |
Mid | Mid(string,start,length) | 從string字符串的start字符開始取得length長度的字符串,如果省略第三個參數(shù)表示從start字符開始到字符串結(jié)尾的字符串 |
Left | Left(string,length) | 從string字符串的左邊取length長度的字符串 |
Right | Right(string,length) | 從srting字符串的右邊取得length長度的字符串 |
LCase | LCase(string) | 將字符串里的所有大寫字母轉(zhuǎn)化成小寫字母 |
UCase | UCase(string) | 將字符串里的小寫字母轉(zhuǎn)化成大寫字母 |
StrComp | Strcomp(string1,string1) | 返回string1字符串與string2字符串的比較結(jié)果,如果兩個字符串相同,返回0 |
InStr | InStr(string1,string2) | 返回string2字符串在string1字符串中第一次出現(xiàn)的位置 |
Split | Split(string1,delimiter | 將字符串根據(jù)delimiter拆分成一維數(shù)組,其中delimiter用于表示子字符串界限的字符,如果省略,使用空格(“”)當作分隔符 |
Replace | Replace(string1,find,replacewith) | 返回字符串,其中指定的子字符串(find)被替換為另一個子字符串(replacewith) |
常用日期和時間函數(shù)及說明
函數(shù) | 語法 | 功能 |
Now | Now() | 取得系統(tǒng)當前的日期和時間 |
Date | Date() | 取得系統(tǒng)當前的日期 |
Time | Time() | 取得系統(tǒng)當前的時間 |
Year | Year() | 取得給定日期的年份 |
Month | Month(Date) | 取得給定日期的月份 |
Day | Day(Date) | 取得給定日期是幾號 |
Hour | Hour(time) | 取得給定時間是第幾小時 |
Minute | Minute(time) | 取得給定時間是第幾分鐘 |
Second | Second(time) | 取得給定時間是第幾秒 |
WeekDay | WeekDay(Date) | 取得給定日期是星期幾的整數(shù) 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) | 轉(zhuǎn)化為短日期格式 |
FortDateTime(Date,vblongDate) | 轉(zhuǎn)化為長日期格式 | |
FortDateTime(Date,vbShortTime) | 轉(zhuǎn)化為短時間格式 | |
FortDateTime(Date,vbLongTime) | 轉(zhuǎn)化為長時間格式 |
日期或時間間隔因子
間隔因子 | yyyy | m | d | ww | h | s |
說明 | 年 | 月 | 日 | 星期 | 小時 | 秒 |
常用的檢驗函數(shù)及功能
函數(shù) | 功能 |
VarType(variant) | 檢查變量vriant的值,函數(shù)值為該變量的數(shù)據(jù)子類型,0表示空,2表示整數(shù),7表示日子,8表示字符串,11表示布爾變量,8192表示數(shù)組 |
IsNumeric(variant) | 檢查變量variant的值,如果variant是數(shù)值類型,則函數(shù)值為ture |
IsNull(variant) | 檢查變量variant的值,如果variant為null,則函數(shù)值為ture |
IsEmpty(variant) | 檢查變量的值,如果variant是empty,則函數(shù)值為ture |
IsObject(variant) | 檢查變量variant的值,如果variant是對象類型,則函數(shù)值為ture |
IsDate(variant) | 檢查變量variant的值,如果variant是日期類型,則函數(shù)值為ture |
IsArray(variant) | 檢查變量variant的值,如果variant是數(shù)組類型,則函數(shù)值為ture |
下面給大家一個常見實例,大家可以運行測試
Option Explicit '*********************************Date/Time函數(shù)******************************* 'CDate函數(shù)把一個合法的日期和事件表達式轉(zhuǎn)換為Date類型,并返回結(jié)果 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函數(shù)返回當前系統(tǒng)的日期 '日期 MsgBox Date '日期+時間 MsgBox Now '時間 MsgBox Time 'DateAdd函數(shù)可返回已添加指定時間間隔的日期 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函數(shù)可返回兩個日期之間的時間間隔數(shù) 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函數(shù)可返回給定日期的指定部分 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函數(shù)可返回指定的年、月、日的子類型Date的Variant MsgBox DateSerial(2012,2,30) 'DateValue函數(shù)返回一個日期類型 MsgBox DateValue("31-Jan-2012 2:39:49 AM") 'Day函數(shù)可返回介于1到31之間的一個代表月的天數(shù)的數(shù)字 MsgBox Day(Date) 'FormatDateTime函數(shù)可格式化并返回一個額合法的日期或時間表達式 MsgBox FormatDateTime(Now,0) MsgBox FormatDateTime(Now,1) MsgBox FormatDateTime(Now,2) MsgBox FormatDateTime(Now,3) MsgBox FormatDateTime(Now,4) 'Hour函數(shù)可返回介于0到23之間的代表天的小時數(shù)的數(shù)字 MsgBox Hour(Now) 'IsDate函數(shù)可返回一個布爾值,指示經(jīng)計算的表達式是否可被轉(zhuǎn)換為日期,如果表達式是日期或可被轉(zhuǎn)換為日期,則返回True,否則,返回False。 MsgBox IsDate("15-3-2012") 'Minute函數(shù)可返回表示小時的分鐘數(shù)的數(shù)字 MsgBox Minute(Now) 'Month函數(shù)可返回表示年的月份的數(shù)字 MsgBox Month(Now) 'Second函數(shù)可返回表示分鐘的秒數(shù)的數(shù)字 MsgBox Second(Now) 'Time函數(shù)可返回當前的系統(tǒng)時間 MsgBox Time 'Timer函數(shù)可返回午夜12時以后已經(jīng)過去的秒數(shù) MsgBox Timer 'TimeSerial函數(shù)可把時、分、秒合并成為時間 MsgBox TimeSerial(3,45,50) 'TimeValue函數(shù)可返回包含時間的日期子類型的變量 MsgBox TimeValue(Now) 'Weekday函數(shù)可返回表示一周的天數(shù)的數(shù)字,介于1和7之間。 MsgBox Weekday(Date,0) 'WeekdayName函數(shù)可返回一周中指定一天的星期名 MsgBox WeekdayName(3) MsgBox WeekdayName(Weekday(Date,1)) 'Year函數(shù)可返回表示年份的一個數(shù)字 MsgBox Year(date) '*********************************Conversion函數(shù)******************************* 'Asc函數(shù)可把字符串中的第一個字母轉(zhuǎn)換為對應(yīng)的ANSI代碼,并返回結(jié)果 MsgBox Asc("A") MsgBox Asc("a") 'CBool函數(shù)可把表達式轉(zhuǎn)換為布爾類型 Dim a a=5 MsgBox CBool(a) 'CByte函數(shù)可把表達式轉(zhuǎn)換為字節(jié)類型 Dim b b=134.12345 MsgBox CByte(b) 'CCur函數(shù)可把表達式轉(zhuǎn)換為貨幣類型 Dim c c=12345.12345656 MsgBox CCur(c) 'CDbl函數(shù)可把表達式轉(zhuǎn)換為雙精度類型 Dim e e=123.12454656577435235436 MsgBox CDbl(e) 'Chr函數(shù)可把指定的ANSI字符代碼轉(zhuǎn)換為字符 MsgBox Chr(65) 'CInt函數(shù)可把表達式轉(zhuǎn)換為整數(shù)類型 MsgBox CInt(12.3435) 'CLng函數(shù)可把表達式轉(zhuǎn)換為長整形類型 MsgBox CLng(12.676) MsgBox CLng(12.243) 'CSng函數(shù)可把表達式轉(zhuǎn)換為單精度類型 MsgBox CSng(122.355465) MsgBox CSng(21.23222) 'CStr函數(shù)可把表達式轉(zhuǎn)換為字符串類型 MsgBox CStr(false) MsgBox CStr(Date) MsgBox CStr(Empty) MsgBox CStr(Error) '*********************************Format函數(shù)******************************* 'FormatCurrency函數(shù)可返回作為貨幣值被格式化的表達式,使用系統(tǒng)控制面板中定義的貨幣符號 MsgBox FormatCurrency(21.23456) 'FormatNumber函數(shù)可返回作為數(shù)字被格式化的表達式 MsgBox FormatNumber(12.23456775,3) 'FormatPercent函數(shù)可返回作為百分數(shù)被格式化的表達式 MsgBox FormatPercent(3/7,3) '*********************************Math函數(shù)******************************* 'Abs函數(shù)可返回指定的數(shù)字的絕對值 MsgBox Abs(-9) 'Atn函數(shù)可返回指定數(shù)字的正切 MsgBox Atn(9) 'Cos函數(shù)可返回指定數(shù)字的余弦 MsgBox Cos(9) 'Exp函數(shù)可e的冪次方 MsgBox Exp(2.3) 'Fix函數(shù)可返回指定數(shù)字的整數(shù)部分 MsgBox Fix(-6.325) MsgBox Int(-6.325) 'Log函數(shù)可返回指定數(shù)據(jù)的自然對數(shù) MsgBox Log(10) 'Rnd函數(shù)可返回一個隨機數(shù),數(shù)字總是小于1但大于或等于0. MsgBox Rnd 'Sgn函數(shù)可返回指定數(shù)字的符號的整數(shù) MsgBox Sgn(15) MsgBox Sgn(-5) MsgBox Sgn(0) 'Sin函數(shù)可返回指定數(shù)字的正弦 MsgBox Sin(9) 'Sqr函數(shù)可返回一個數(shù)的平方根 MsgBox Sqr(9) 'Tan函數(shù)可返回指定數(shù)字的正切 MsgBox Tan(9) '*********************************Array函數(shù)******************************* 'Array可返回一個包含數(shù)組的變量 Dim Arr Arr=Array(1,2,3,4,5) MsgBox Arr(0) 'Filter函數(shù)可返回一個基于0的數(shù)組,此數(shù)組包含以特定過濾條件為基礎(chǔ)的字符串數(shù)組的子集 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函數(shù)可返回一個指示指定的變量是否為數(shù)組的布爾值。如果變量為數(shù)組,則返回True,否則返回False。 Dim a(3) a(0)=1 a(1)=2 a(2)=3 MsgBox IsArray(a) 'Join函數(shù)可返回一個由某個數(shù)組中一系列子字符串組成的字符串 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函數(shù)可返回指示數(shù)組維數(shù)的最小下標。(始終為0) 'UBound函數(shù)可返回指示數(shù)組維數(shù)的最大下標。 Dim a(5) MsgBox LBound(a) MsgBox UBound(a) 'Split函數(shù)可返回基于0的一維數(shù)組,此數(shù)組包含指定的子字符串 Dim txt,a txt="hello/world!" a=Split(txt,"/") MsgBox a(0) MsgBox a(1) '*********************************String函數(shù)******************************* 'InStr函數(shù)可返回一個字符串在另一個字符串中首次出現(xiàn)的位置 Dim txt,pos txt="This is a beautiful day!" pos=InStr(4,txt,"is",1) MsgBox pos 'InStrRev函數(shù)可返回一個字符串在另一個字符串中首次出現(xiàn)的位置,搜索從字符串的末端開始,但是返回的位置是從字符串的起點開始計數(shù)的。 Dim txt,pos txt="This is a beautiful day!" pos=InStrRev(txt,"i",-1,1) MsgBox pos 'LCase函數(shù)可把指定的字符串轉(zhuǎn)換為小寫 'UCase函數(shù)可把指定的字符串轉(zhuǎn)換為大寫 Dim txt,pos1,pos2 txt="This Is A Beautiful Day!" pos1=LCase(txt) pos2=UCase(txt) MsgBox pos1 MsgBox pos2 'Left函數(shù)可從字符串的左側(cè)返回指定數(shù)目的字符 'Right函數(shù)可從字符串的右側(cè)返回指定數(shù)目的字符 Dim txt,pos1,pos2 txt="This is a beautiful day!" pos1=Left(txt,5) pos2=Right(txt,6) MsgBox pos1 MsgBox pos2 'Len函數(shù)可返回字符串中字符的數(shù)目 Dim txt,pos txt="This is a beautiful day!" pos=Len(txt) MsgBox pos 'LTrim函數(shù)可刪除字符串左側(cè)的空格 'RTrim函數(shù)可刪除字符串右側(cè)的空格 'Trim函數(shù)可刪除字符串兩端的空格 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函數(shù)可從字符串中返回指定數(shù)目的字符 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函數(shù)可使用一個字符串替換另一個字符串指定的次數(shù) Dim txt txt="This is a beautiful day!" MsgBox Replace(txt,"is","AA",4,2) 'Space函數(shù)可返回一個由指定數(shù)目的空格組成的字符串 Dim txt txt=Space(20) MsgBox txt MsgBox Len(txt) 'StrComp函數(shù)可比較兩個字符串,并返回表示比較結(jié)果的一個值 Dim txt1,txt2 txt1="hello" txt2="hell" MsgBox StrComp(txt1,txt2) 'String函數(shù)可返回包含指定長度的重復(fù)字符的一個字符串 MsgBox String(10,"*") 'StrReverse函數(shù)可反轉(zhuǎn)一個字符串 Dim txt txt="This is a beautiful day!" MsgBox StrReverse(txt) '*********************************其他函數(shù)******************************* 'InputBox函數(shù)可顯示一個對話框,用戶可在其中輸入文本或點擊一個按鈕。 Dim fname fname=InputBox("Enter your name:") MsgBox ("Your name is "fname) 'IsEmpty函數(shù)可返回指定的變量是否被初始化的布爾值。 Dim x,y y=10 MsgBox IsEmpty(x) MsgBox IsEmpty(y) 'IsNull函數(shù)可返回指定表達式是否無效數(shù)據(jù)的布爾值 Dim x,y,z x=Null y=10 z="" MsgBox IsNull(x) MsgBox IsNull(y) MsgBox IsNull(z) 'IsNumeric函數(shù)可返回指示指定的表達式是否可作為數(shù)字來計算的布爾值。 Dim x x=10 MsgBox IsNumeric(x) x=Empty MsgBox IsNumeric(x) x="1 h" MsgBox IsNumeric(x)
這篇關(guān)于vbscript常見函數(shù)的文章就介紹到這里,希望大家以后多多支持腳本之家。
標簽:湖州 文山 西安 銅川 四平 邢臺 鶴壁 ???/a>
巨人網(wǎng)絡(luò)通訊聲明:本文標題《VBS基礎(chǔ)篇 - vbscript常用函數(shù)及功能》,本文關(guān)鍵詞 VBS,基礎(chǔ),篇,vbscript,常用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。