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

相关推荐
2401_841495645 小时前
【数据结构】基于BF算法的树种病毒检测
java·数据结构·c++·python·算法·字符串·模式匹配
課代表19 小时前
JavaScript 中获取二维数组最大值
javascript·max·数组·递归·array·最大值·二维
SamHou01 天前
奶奶都能看懂的 C++ —— 数组与指针
指针·数组·cpp
課代表3 天前
VB.NET 操作 INI 文件类
api·配置文件·文本·vb.net·ini·kernel32·
課代表9 天前
VB.NET 与 C# 文件操作文本到二进制文件的读写
c#·二进制文件·vb.net·streamwriter·文本文件·读写·streamreader
苏纪云13 天前
数据结构<C++>——数组
java·数据结构·c++·数组·动态数组
云计算练习生16 天前
linux shell编程实战 03 数组:批量处理数据
linux·运维·服务器·数组·shell编程
狮子座的男孩16 天前
js基础:06、函数(创建函数、参数、返回值、return、立即执行函数、对象(函数))和枚举对象的属性
开发语言·前端·javascript·经验分享·函数·枚举对象·立即执行函数
胡西风_foxww17 天前
git 添加除了包含特定字符串的文件
git·字符串·文件·add·添加·特定·包含