VB.Net 常用函数

一、概述

Visual Basic .NET(VB.Net)是一种面向对象的编程语言,广泛应用于Windows桌面程序、Web应用程序及数据库开发。掌握其内置函数是提高开发效率的关键。本文将系统介绍VB.Net中常用的函数,包括类型转换、字符串处理、日期时间操作、数学计算等,并提供丰富的代码示例和注释,帮助开发者快速上手。


二、常用函数分类详解

1. 类型转换函数

VB.Net 提供了一系列以 C 开头的类型转换函数,用于将表达式转换为指定类型。

vbnet 复制代码
Dim num As String = "123"
Dim intNum As Integer = CInt(num)  ' 将字符串转换为整型
Dim dblNum As Double = CDbl(num)   ' 将字符串转换为双精度浮点型
Dim str As String = CStr(123)      ' 将数字转换为字符串

2. 数学函数

常用的数学函数包括 AbsSinCosSqrt 等,用于数值计算。

vbnet 复制代码
Dim a As Integer = -10
Dim absValue = Math.Abs(a)  ' 取绝对值,结果为10

Dim angle As Double = Math.PI / 4
Dim sinValue = Math.Sin(angle)  ' 计算正弦值

Dim number As Double = 16
Dim sqrtValue = Math.Sqrt(number)  ' 计算平方根,结果为4

3. 字符串处理函数

VB.Net 提供了丰富的字符串处理函数,如 LenLeftRightMidReplace 等。

vbnet 复制代码
Dim s As String = "Hello, World!"
Dim length As Integer = Len(s)  ' 获取字符串长度

Dim leftPart As String = Left(s, 5)  ' 取左边5个字符:"Hello"
Dim rightPart As String = Right(s, 6)  ' 取右边6个字符:"World!"

Dim midPart As String = Mid(s, 8, 5)  ' 从第8个字符开始取5个字符:"World"

Dim newStr As String = Replace(s, "World", "VB.Net")  ' 替换字符串

4. 日期时间函数

常用的日期时间函数包括 NowTodayDateAddDateDiff 等。

vbnet 复制代码
Dim currentTime As DateTime = Now()  ' 获取当前日期和时间
Dim todayDate As DateTime = Today()  ' 获取当前日期(时间部分为00:00:00)

Dim nextWeek As DateTime = DateAdd(DateInterval.Day, 7, currentTime)  ' 加7天

Dim daysDiff As Long = DateDiff(DateInterval.Day, todayDate, nextWeek)  ' 计算天数差

5. 文件操作函数

VB.Net 支持基本的文件操作,如 FileLenFileDateTimeGetAttr 等。

vbnet 复制代码
Dim filePath As String = "C:\test.txt"
Dim fileSize As Long = FileLen(filePath)  ' 获取文件大小(字节)
Dim createTime As DateTime = FileDateTime(filePath)  ' 获取文件创建时间
Dim attr As FileAttribute = GetAttr(filePath)  ' 获取文件属性

6. 数组与集合函数

常用的数组函数包括 FilterJoinSplitUBound 等。

vbnet 复制代码
Dim arr() As String = {"Apple", "Banana", "Cherry"}
Dim filtered() As String = Filter(arr, "a", True, CompareMethod.Text)  ' 筛选包含"a"的元素

Dim combined As String = Join(arr, ", ")  ' 合并为字符串:"Apple, Banana, Cherry"

Dim sArr() As String = Split("A,B,C", ",")  ' 分割字符串为数组
Dim upperBound As Integer = UBound(sArr)  ' 获取数组最大索引

三、高级函数示例

1. 使用 CallByName 动态调用方法

vbnet 复制代码
Public Class Calculator
    Public Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
        Return a + b
    End Function
End Class

Dim calc As New Calculator()
Dim result As Integer = CallByName(calc, "Add", CallType.Method, 3, 5)  ' 动态调用Add方法

2. 使用 IIf 进行条件判断

vbnet 复制代码
Dim score As Integer = 85
Dim result As String = IIf(score >= 60, "及格", "不及格")  ' 条件判断

四、Mermaid 函数关系图

以下是一个常用函数分类的 UML 类图(简化表示):
VBNetFunctions +Abs(number) +Asc(String) +CInt(expression) +CStr(expression) +Len(String) +Now() +Replace(expression, find, replace) +DateDiff(interval, date1, date2) +FileLen(pathname) <<Category>> MathFunctions +Sin(number) +Cos(number) +Sqrt(number) <<Category>> StringFunctions +Left(string, length) +Right(string, length) +Mid(string, start, length) <<Category>> DateTimeFunctions +DateAdd(interval, number, date) +DateDiff(interval, date1, date2)


五、生词表(便于学习)

单词/短语 音标 词性 词根/词缀 释义 搭配 例子
Abs /æbs/ n. 缩写自 absolute 绝对值 Abs function Abs(-5)=5
Asc /æsk/ n. ASCII ASCII码 Asc function Asc("A")=65
CInt /siː ɪnt/ v. Convert to Integer 转换为整型 CInt(expression) CInt("123")
Len /len/ n. Length 长度 Len(string) Len("Hello")=5
Replace /rɪˈpleɪs/ v. Re- + place 替换 Replace(expr, find, rep) Replace("A","B","C")
DateDiff /deɪt dɪf/ n. Date + Difference 日期差 DateDiff(interval, d1, d2) DateDiff("d", #1/1#, #1/2#)=1
FileLen /faɪl len/ n. File + Length 文件长度 FileLen(path) FileLen("test.txt")
UBound /juː baʊnd/ n. Upper Bound 上界 UBound(array) UBound(arr)
IIf /aɪ aɪ ef/ n. Immediate If 立即判断 IIf(expr, true, false) IIf(True, "Yes", "No")

相关推荐
加成BUFF17 小时前
C++入门讲解3:数组与指针全面详解
开发语言·c++·算法·指针·数组
加油=^_^=17 小时前
【C++11】特殊类设计 | 类型转换
c++·单例模式·类型转换
oscar9992 天前
CSP-J教程——第一阶段第九、十课:数组与字符串
字符串·数组·csp-j
oscar9993 天前
CSP-J教程——第一阶段第十一课:函数与递归初步
递归·函数·csp-j
埃伊蟹黄面3 天前
字符串算法精要与例题汇编
c++·算法·leetcode·字符串
一雨方知深秋4 天前
二.java程序基本语法
java·类型转换·变量·方法·运算符·字面量·关键字标识符
2401_841495646 天前
【LeetCode刷题】跳跃游戏
数据结构·python·算法·leetcode·游戏·贪心算法·数组
好评1247 天前
C++ 字符串:始于 char*,终于深拷贝
开发语言·c++·stl·字符串
2401_841495647 天前
【LeetCode刷题】缺失的第一个正数
数据结构·python·算法·leetcode·数组·哈希·缺失最小正整数
BestOrNothing_20158 天前
C++ 函数类型大全:成员函数 / 非成员函数 / 全局函数 / 静态函数 / 特殊成员函数 / 虚函数 / 模板函数 全面总结
c++·面向对象·八股·函数·程序语言