VB 函数

按类别整理的内置函数速查:字符串、数学、日期、类型、数组、格式化。

Intermediate
Key PointsVBScript 的内置函数不多,但每个都有细节:Mid 从 1 开始、Round 是银行家舍入、DateDiff 的单位字符串。

字符串

FunctionReturnsNote
Len(s)字符数中文字符算 1
Left(s, n)前 n 个字符n 超过长度返回整串
Right(s, n)后 n 个字符
Mid(s, start, len)从 start 起 len 个start 从 1 开始
InStr(s, sub)位置,找不到 0从 1 开始
InStrRev(s, sub)最后一个位置
Replace(s, a, b)新字符串全部替换
Split(s, d)数组返回 0 基数组
Join(arr, d)字符串
Trim(s)两端去空格LTrim / RTrim 单边
UCase / LCase转换后字符串
String(n, c)n 个 c 组成String(3, "-") → ---
StrReverse(s)倒序字符串
VBSCRIPT
Dim s
s = "VBScript is easy"

WScript.Echo Len(s)                  ' 16
WScript.Echo Left(s, 8)              ' VBScript
WScript.Echo Mid(s, 10, 2)           ' is
WScript.Echo InStr(s, "easy")        ' 13
WScript.Echo Replace(s, "easy", "fun")
WScript.Echo UCase(Left(s, 8))
WScript.Echo String(5, "-")
WScript.Echo StrReverse("abc")
Output
16
VBScript
is
13
VBScript is fun
VBSCRIPT
-----
cba

Mid's start index is 1, not 0; Mid(s, 0, 2) raises an error. This trips up anyone coming from JavaScript's substring or Python slicing.

数学

FunctionReturnsNote
Abs(n)非负数
Int(n)≤ n 的整数负数向下
Fix(n)去掉小数部分负数向上
Round(n, d)d 位小数银行家舍入
Sqr(n)
Rnd()0–1 之间需先 Randomize
Int(Rnd()*n)+11–n
VBSCRIPT
WScript.Echo Int(3.7)        ' 3
WScript.Echo Int(-3.7)       ' -4
WScript.Echo Fix(-3.7)       ' -3
WScript.Echo Round(2.5)      ' 2(银行家舍入)
WScript.Echo Round(3.5)      ' 4
WScript.Echo Round(1.2345, 2) ' 1.23

Randomize
WScript.Echo Int(Rnd() * 100) + 1
Output
3
-4
-3
2
4
1.23
(1–100 之间的随机整数)

Round(2.5) yields 2, not 3: VBScript uses banker's rounding (round half to even). That matches accounting practice but not intuition; for conventional rounding use Int(x + 0.5).

日期

FunctionReturnsExample
Now()Date + Time2026/11/3 14:25:09
Date() / Time()各自部分
Year / Month / Day数字2026 / 11 / 3
Hour / Minute / Second数字14 / 25 / 9
Weekday(d)1–7(周日=1)
DateAdd(u, n, d)新日期DateAdd("d", 7, Date())
DateDiff(u, d1, d2)数字DateDiff("d", d1, d2)
DatePart(u, d)数字DatePart("q", d)
DateSerial(y,m,d)日期DateSerial(2026,11,3)
VBSCRIPT
Dim d
d = DateSerial(2026, 11, 3)

WScript.Echo Year(d) & "-" & Month(d) & "-" & Day(d)
WScript.Echo DateAdd("d", 30, d)
WScript.Echo DateDiff("d", d, DateSerial(2027, 1, 1))
WScript.Echo DatePart("q", d)
Output
2026-11-3
2026/12/3
59
4

DateAdd units are strings: "yyyy", "q", "m", "d", "h", "n" (minutes!), "s". Note "m" is months and "n" is minutes — a trap nearly everyone hits.

类型与判断

FunctionPurpose
CStr(v)CStr(123) → "123"
CInt(v)范围 −32768..32767
CLng(v)范围更大
CDbl(v)小数
CDate(v)字符串转日期
CBool(v)0 假,非 0 真
IsNull(v)明确的无值
IsEmpty(v)声明未赋值
IsNumeric(v)能否当数字
IsDate(v)能否当日期
IsArray(v)
IsObject(v)
TypeName(v)"Integer" 等
VBSCRIPT
WScript.Echo CInt("42") + 1
WScript.Echo IsNumeric("abc")
WScript.Echo IsNumeric("3.14")
WScript.Echo TypeName(Now())
WScript.Echo CDate("2026-11-03")
Output
43
False
True
Date
2026/11/3

CInt on an out-of-range value raises an overflow error rather than promoting to a long. Use CLng or CDbl for large values, and guard with IsNumeric to avoid type-mismatch errors.

格式化

VBSCRIPT
WScript.Echo FormatNumber(1234.5, 2)        ' 1,234.50
WScript.Echo FormatCurrency(1234.5)         ' ¥1,234.50
WScript.Echo FormatPercent(0.256, 1)        ' 25.6%
WScript.Echo FormatDateTime(Now(), 2)       ' 2026-11-03
WScript.Echo FormatDateTime(Now(), 1)       ' 2026年11月3日
Output
1,234.50
¥1,234.50
25.6%
2026-11-03
2026年11月3日
FormatCurrency and FormatNumber follow the system locale: the same script prints ¥1,234.50 on Chinese Windows and possibly $1,234.50 on English. Never rely on these for data exchange — store the raw value and format only for display.

Friends & Partners