在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", "Yiibai", "Yiibai"}
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 Yiibai Yiibai
Message: Message sent at 17:58 on 2018年12月12日
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 |
比較兩個指定的字串物件,並返回一個整數,以整理順序指示它們的相對位置。但是,如果布林引數為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