VB Functions
8 methodsVisual Basic 核心函数与语句集合。
Console.WriteLine(s)将文本写入标准输出并换行。
Parameters
| Name | Type | Description |
|---|---|---|
| s | String / 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
| Name | Type | Description |
|---|---|---|
| value | any | 待转换值 |
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
| Name | Type | Description |
|---|---|---|
| s | String | 字符串 |
Returns
Integer,字符数
Example
vb
Dim n As Integer = Len("hello") ' 5
Dim m As Integer = Len("VB") ' 2Mid(s, start, length)从指定位置(1-based)截取指定长度子串。
Parameters
| Name | Type | Description |
|---|---|---|
| s | String | 源字符串 |
| start | Integer | 起始位置(1-based) |
| length | Integer | 截取长度 |
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
| Name | Type | Description |
|---|---|---|
| s | String | 源字符串 |
| substring | String | 待查找子串 |
Returns
Integer,位置(1-based),0 表示未找到
Example
vb
Dim p As Integer = InStr("hello", "ll") ' 3
Dim q As Integer = InStr("hello", "z") ' 0If...Then...Else条件语句,根据布尔表达式选择执行分支。
Parameters
| Name | Type | Description |
|---|---|---|
| condition | Boolean | 条件表达式 |
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
| Name | Type | Description |
|---|---|---|
| collection | IEnumerable | 可枚举集合 |
Returns
无,循环执行
Example
vb
Dim names() As String = {"Alice", "Bob", "Carol"}
For Each name As String In names
Console.WriteLine(name)
Next