- VB.Net 基础教程
- VB.Net - 首页
- VB.Net - 概述
- VB.Net - 环境设置
- VB.Net - 程序结构
- VB.Net - 基本语法
- VB.Net - 数据类型
- VB.Net - 变量
- VB.Net - 常量
- VB.Net - 修饰符
- VB.Net - 语句
- VB.Net - 指令
- VB.Net - 运算符
- VB.Net - 决策制定
- VB.Net - 循环
- VB.Net - 字符串
- VB.Net - 日期和时间
- VB.Net - 数组
- VB.Net - 集合
- VB.Net - 函数
- VB.Net - 子过程
- VB.Net - 类和对象
- VB.Net - 异常处理
- VB.Net - 文件处理
- VB.Net - 基本控件
- VB.Net - 对话框
- VB.Net - 高级窗体
- VB.Net - 事件处理
- VB.Net 高级教程
- VB.Net - 正则表达式
- VB.Net - 数据库访问
- VB.Net - Excel 表格
- VB.Net - 发送电子邮件
- VB.Net - XML 处理
- VB.Net - Web 编程
- VB.Net 有用资源
- VB.Net - 快速指南
- VB.Net - 有用资源
- VB.Net - 讨论
VB.Net - 字符串
在 VB.Net 中,您可以使用字符串作为字符数组,但是,更常见的做法是使用 String 关键字声明字符串变量。String 关键字是 System.String 类的别名。
创建字符串对象
您可以使用以下方法之一创建字符串对象:
将字符串文字分配给 String 变量
使用 String 类构造函数
使用字符串连接运算符 (+)
检索返回字符串的属性或调用方法
调用格式化方法将值或对象转换为其字符串表示形式
以下示例演示了这一点:
Module strings
Sub Main()
Dim fname, lname, fullname, greetings As String
fname = "Rowan"
lname = "Atkinson"
fullname = fname + " " + lname
Console.WriteLine("Full Name: {0}", fullname)
'by using string constructor
Dim letters As Char() = {"H", "e", "l", "l", "o"}
greetings = New String(letters)
Console.WriteLine("Greetings: {0}", greetings)
'methods returning String
Dim sarray() As String = {"Hello", "From", "Tutorials", "Point"}
Dim message As String = String.Join(" ", sarray)
Console.WriteLine("Message: {0}", message)
'formatting method to convert a value
Dim waiting As DateTime = New DateTime(2012, 12, 12, 17, 58, 1)
Dim chat As String = String.Format("Message sent at {0:t} on {0:D}", waiting)
Console.WriteLine("Message: {0}", chat)
Console.ReadLine()
End Sub
End Module
编译并执行上述代码后,将产生以下结果:
Full Name: Rowan Atkinson Greetings: Hello Message: Hello From Tutorials Point Message: Message sent at 5:58 PM on Wednesday, December 12, 2012
String 类的属性
String 类具有以下两个属性:
| 序号 | 属性名称和描述 |
|---|---|
| 1 |
Chars 获取当前 String 对象中指定位置的 Char 对象。 |
| 2 |
Length 获取当前 String 对象中的字符数。 |
String 类的方法
String 类有许多方法可以帮助您处理字符串对象。下表提供了一些最常用的方法:
| 序号 | 方法名称和描述 |
|---|---|
| 1 |
Public Shared Function Compare ( strA As String, strB As String ) As Integer 比较两个指定的字符串对象,并返回一个整数,指示它们在排序顺序中的相对位置。 |
| 2 |
Public Shared Function Compare ( strA As String, strB As String, ignoreCase As Boolean ) As Integer 比较两个指定的字符串对象,并返回一个整数,指示它们在排序顺序中的相对位置。但是,如果 Boolean 参数为 true,则忽略大小写。 |
| 3 |
Public Shared Function Concat ( str0 As String, str1 As String ) As String 连接两个字符串对象。 |
| 4 |
Public Shared Function Concat ( str0 As String, str1 As String, str2 As String ) As String 连接三个字符串对象。 |
| 5 |
Public Shared Function Concat (str0 As String, str1 As String, str2 As String, str3 As String ) As String 连接四个字符串对象。 |
| 6 |
Public Function Contains ( value As String ) As Boolean 返回一个值,指示指定的字符串对象是否出现在此字符串中。 |
| 7 |
Public Shared Function Copy ( str As String ) As String 创建一个新的 String 对象,其值与指定的字符串相同。 |
| 8 |
pPublic Sub CopyTo ( sourceIndex As Integer, destination As Char(), destinationIndex As Integer, count As Integer ) 将字符串对象指定位置的指定数量的字符复制到 Unicode 字符数组的指定位置。 |
| 9 |
Public Function EndsWith ( value As String ) As Boolean 确定字符串对象的结尾是否与指定的字符串匹配。 |
| 10 |
Public Function Equals ( value As String ) As Boolean 确定当前字符串对象和指定的字符串对象是否具有相同的值。 |
| 11 |
Public Shared Function Equals ( a As String, b As String ) As Boolean 确定两个指定的字符串对象是否具有相同的值。 |
| 12 |
Public Shared Function Format ( format As String, arg0 As Object ) As String 用指定对象的字符串表示形式替换指定字符串中一个或多个格式项。 |
| 13 |
Public Function IndexOf ( value As Char ) As Integer 返回当前字符串中指定 Unicode 字符第一次出现的从零开始的索引。 |
| 14 |
Public Function IndexOf ( value As String ) As Integer 返回此实例中指定字符串第一次出现的从零开始的索引。 |
| 15 |
Public Function IndexOf ( value As Char, startIndex As Integer ) As Integer 返回此字符串中指定 Unicode 字符第一次出现的从零开始的索引,从指定的字符位置开始搜索。 |
| 16 |
Public Function IndexOf ( value As String, startIndex As Integer ) As Integer 返回此实例中指定字符串第一次出现的从零开始的索引,从指定的字符位置开始搜索。 |
| 17 |
Public Function IndexOfAny ( anyOf As Char() ) As Integer 返回此实例中指定 Unicode 字符数组中任何字符第一次出现的从零开始的索引。 |
| 18 |
Public Function IndexOfAny ( anyOf As Char(), startIndex As Integer ) As Integer 返回此实例中指定 Unicode 字符数组中任何字符第一次出现的从零开始的索引,从指定的字符位置开始搜索。 |
| 19 |
Public Function Insert ( startIndex As Integer, value As String ) As String 返回一个新字符串,其中在当前字符串对象的指定索引位置插入了指定的字符串。 |
| 20 |
Public Shared Function IsNullOrEmpty ( value As String ) As Boolean 指示指定的字符串是否为空或空字符串。 |
| 21 |
Public Shared Function Join ( separator As String, ParamArray value As String() ) As String 使用指定的间隔符连接字符串数组的所有元素。 |
| 22 |
Public Shared Function Join ( separator As String, value As String(), startIndex As Integer, count As Integer ) As String 使用指定的间隔符连接字符串数组的指定元素。 |
| 23 |
Public Function LastIndexOf ( value As Char ) As Integer 返回当前字符串对象中指定 Unicode 字符最后一次出现的从零开始的索引位置。 |
| 24 |
Public Function LastIndexOf ( value As String ) As Integer 返回当前字符串对象中指定字符串最后一次出现的从零开始的索引位置。 |
| 25 |
Public Function Remove ( startIndex As Integer ) As String 删除当前实例中从指定位置开始到最后位置的所有字符,并返回字符串。 |
| 26 |
Public Function Remove ( startIndex As Integer, count As Integer ) As String 删除当前字符串中从指定位置开始的指定数量的字符,并返回字符串。 |
| 27 |
Public Function Replace ( oldChar As Char, newChar As Char ) As String 将当前字符串对象中指定 Unicode 字符的所有出现替换为指定的 Unicode 字符,并返回新字符串。 |
| 28 |
Public Function Replace ( oldValue As String, newValue As String ) As String 将当前字符串对象中指定字符串的所有出现替换为指定的字符串,并返回新字符串。 |
| 29 |
Public Function Split ( ParamArray separator As Char() ) As String() 返回一个字符串数组,其中包含当前字符串对象中的子字符串,这些子字符串由指定 Unicode 字符数组的元素分隔。 |
| 30 |
Public Function Split ( separator As Char(), count As Integer ) As String() 返回一个字符串数组,其中包含当前字符串对象中的子字符串,这些子字符串由指定 Unicode 字符数组的元素分隔。int 参数指定要返回的最大子字符串数。 |
| 31 |
Public Function StartsWith ( value As String ) As Boolean 确定此字符串实例的开头是否与指定的字符串匹配。 |
| 32 |
Public Function ToCharArray As Char() 返回一个 Unicode 字符数组,其中包含当前字符串对象中的所有字符。 |
| 33 |
Public Function ToCharArray ( startIndex As Integer, length As Integer ) As Char() 返回一个 Unicode 字符数组,其中包含当前字符串对象中的所有字符,从指定的索引开始到指定的长度。 |
| 34 |
Public Function ToLower As String 返回转换为小写的此字符串的副本。 |
| 35 |
Public Function ToUpper As String 返回转换为大写的此字符串的副本。 |
| 36 |
Public Function Trim As String 删除当前 String 对象中所有前导和尾随空格字符。 |
以上方法列表并不详尽,请访问 MSDN 库以获取完整的方法和 String 类构造函数列表。
示例
以下示例演示了上面提到的某些方法:
比较字符串
Module strings
Sub Main()
Dim str1, str2 As String
str1 = "This is test"
str2 = "This is text"
If (String.Compare(str1, str2) = 0) Then
Console.WriteLine(str1 + " and " + str2 + " are equal.")
Else
Console.WriteLine(str1 + " and " + str2 + " are not equal.")
End If
Console.ReadLine()
End Sub
End Module
编译并执行上述代码后,将产生以下结果:
This is test and This is text are not equal.
字符串包含字符串
Module strings
Sub Main()
Dim str1 As String
str1 = "This is test"
If (str1.Contains("test")) Then
Console.WriteLine("The sequence 'test' was found.")
End If
Console.ReadLine()
End Sub
End Module
编译并执行上述代码后,将产生以下结果:
The sequence 'test' was found.
获取子字符串
Module strings
Sub Main()
Dim str As String
str = "Last night I dreamt of San Pedro"
Console.WriteLine(str)
Dim substr As String = str.Substring(23)
Console.WriteLine(substr)
Console.ReadLine()
End Sub
End Module
编译并执行上述代码后,将产生以下结果:
Last night I dreamt of San Pedro San Pedro.
连接字符串
Module strings
Sub Main()
Dim strarray As String() = {
"Down the way where the nights are gay",
"And the sun shines daily on the mountain top",
"I took a trip on a sailing ship",
"And when I reached Jamaica",
"I made a stop"
}
Dim str As String = String.Join(vbCrLf, strarray)
Console.WriteLine(str)
Console.ReadLine()
End Sub
End Module
编译并执行上述代码后,将产生以下结果:
Down the way where the nights are gay And the sun shines daily on the mountain top I took a trip on a sailing ship And when I reached Jamaica I made a stop