- 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