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")

相关推荐
布莱克6054 天前
数组详解:定义、作用、应用场景及与链表的区别(C/C++ 代码讲解)
c语言·开发语言·c++·数组
Tisfy6 天前
LeetCode 3720.大于目标字符串的最小字典序排列:状态机 —— :从左往右枚举,失败则退回(最多退回一次)
linux·数据库·leetcode·字符串·状态机·构造
csdn_aspnet6 天前
mysql 函数、存储过程写法及示例
数据库·mysql·存储过程·函数·function·procedure
qq_161111277 天前
深入理解Python中的Contextlib库
python·装饰器·函数·上下文管理器·contextlib
Tisfy17 天前
LeetCode 3090.每个字符最多出现两次的最长子字符串:二重循环 / 滑动窗口
算法·leetcode·字符串·题解·模拟·双指针·滑动窗口
wabs66617 天前
关于字符串【力扣541.反转字符串II的思考】
数据结构·算法·leetcode·字符串
tianyu2341 个月前
MySQL字符串处理函数完全指南:从入门到实战
mysql·字符串·函数·截取
hansang_IR1 个月前
【题解】LC:Z 算法(Z Algorithm)
c++·算法·字符串
Tisfy1 个月前
LeetCode 3014.输入单词需要的最少按键次数 I:遍历 / if-else计算(比纯数学公式写起来麻烦但好想)
数学·算法·leetcode·字符串·题解·贪心
Tisfy1 个月前
LeetCode 3517.最小回文排列 I:排序(Python两行版) / 计数(O(n)时间+O(C)空间+字符串原地修改)
c语言·python·leetcode·字符串·排序·计数排序·回文