在VB.Net中,可以使用字串作為字元數組,但更常見的做法是使用String
關鍵字來聲明一個字串變數。String
關鍵字是System.String
類的別名。
創建一個字串對象
可以使用以下方法之一創建字串對象:
- 通過將一個字串文字分配給一個
String
變數 - 通過使用一個
String
類的構造函數 - 通過使用字串連接運算符(+)
- 通過檢索一個屬性或調用返回一個字串的方法
- 通過調用格式化方法將值或對象轉換為其字串表示形式
以下示例演示了如何創建對象:
Module strings
Sub Main()
Dim fname, lname, fullname, greetings As String
fname = "Bryant"
lname = "Kobe"
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", "zaixian", "zaixian"}
Dim message As String = String.Join(" ", sarray)
Console.WriteLine("Message: {0}", message)
'formatting method to convert a value '
Dim waiting As DateTime = New DateTime(2018, 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
執行上面示例代碼,得到以下結果 -
F:\worksp\vb.net\string>vbc strings.vb
F:\worksp\vb.net\string>strings.exe
Full Name: Bryant Kobe
Greetings: Hello
Message: Hello From zaixian zaixian
Message: Message sent at 17:58 on 2018年12月12日
String類的屬性
String
類具有以下兩個屬性:
編號 | 屬性名稱 | 說明 |
---|---|---|
1 | Chars |
獲取當前String 對象中指定位置的Char 對象。 |
2 | Length |
獲取當前String 對象中的字元數量。 |
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 |
比較兩個指定的字串對象,並返回一個整數,以整理順序指示它們的相對位置。但是,如果布爾參數為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 | Public 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字元數組,其中包含當前字串object 中的所有字元 |
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 |
返回轉換為大寫字串的副本。 |
37 | 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