VB.Net字符串
在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類的屬性
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