Skip to content

Visual Basic Functions API

Visual Basic 内置函数与语句,涵盖 IO、字符串、类型转换与控制流。

1 class · 8 methods

VB Functions

8 methods

Visual Basic 核心函数与语句集合。

Console.WriteLine(s)

将文本写入标准输出并换行。

Parameters

NameTypeDescription
sString / Object待输出内容

Returns

Example

vb
Console.WriteLine("Hello, VB")
Console.WriteLine(3.14)
Console.WriteLine("sum = " & (1 + 2))
Console.ReadLine()

从标准输入读取一行文本。

Returns

String,输入行(EOF 返回 Nothing)

Example

vb
Dim line As String
line = Console.ReadLine()
Console.WriteLine("you typed: " & line)
CInt(s) / CStr(n)

类型转换函数,CInt 转整数,CStr 转字符串。

Parameters

NameTypeDescription
valueany待转换值

Returns

Integer 或 String

Example

vb
Dim n As Integer = CInt("123")
Dim s As String = CStr(456)
Dim d As Double = CDbl("3.14")
Len(s)

返回字符串字符数。

Parameters

NameTypeDescription
sString字符串

Returns

Integer,字符数

Example

vb
Dim n As Integer = Len("hello")   ' 5
Dim m As Integer = Len("VB")       ' 2
Mid(s, start, length)

从指定位置(1-based)截取指定长度子串。

Parameters

NameTypeDescription
sString源字符串
startInteger起始位置(1-based)
lengthInteger截取长度

Returns

String,子串

Example

vb
Dim s As String = Mid("hello world", 7, 5)  ' "world"
Dim t As String = Mid("hello", 2, 3)         ' "ell"
InStr(s, substr)

返回子串首次出现位置(1-based),未找到返回 0。

Parameters

NameTypeDescription
sString源字符串
substringString待查找子串

Returns

Integer,位置(1-based),0 表示未找到

Example

vb
Dim p As Integer = InStr("hello", "ll")   ' 3
Dim q As Integer = InStr("hello", "z")     ' 0
If...Then...Else

条件语句,根据布尔表达式选择执行分支。

Parameters

NameTypeDescription
conditionBoolean条件表达式

Returns

无,控制流

Example

vb
Dim x As Integer = 10
If x > 5 Then
  Console.WriteLine("big")
Else
  Console.WriteLine("small")
End If

' 三元形式
Dim label As String = If(x > 5, "big", "small")
For Each...Next

遍历集合或数组的每个元素。

Parameters

NameTypeDescription
collectionIEnumerable可枚举集合

Returns

无,循环执行

Example

vb
Dim names() As String = {"Alice", "Bob", "Carol"}
For Each name As String In names
  Console.WriteLine(name)
Next