在VB.Net中,可以使用字符串作為字符數(shù)組,但是更常見的做法是使用String關(guān)鍵字聲明一個字符串變量。 string關(guān)鍵字是System.String類的別名。
您可以使用以下方法之一創(chuàng)建字符串對象:
通過指定一個字符串給一個字符串變量
通過使用String類構(gòu)造函數(shù)
通過使用字符串連接運算符(+)
通過檢索屬性或調(diào)用返回字符串的方法
通過調(diào)用格式化方法將值或?qū)ο筠D(zhuǎn)換為其字符串表示形式
下面的例子說明了這一點:
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
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
Full Name: Rowan Atkinson Greetings: Hello Message: Hello From Tutorials Point Message: Message sent at 5:58 PM on Wednesday, December 12, 2012
String類有以下兩個屬性:
SN | 屬性名稱和說明 |
---|---|
1 | Chars 獲取當(dāng)前String對象中指定位置的Char對象。 |
2 | Length 獲取當(dāng)前String對象中的字符數(shù)。 |
String類有許多方法可以幫助你處理字符串對象。 下表提供了一些最常用的方法:
SN | 方法名稱和說明 |
---|---|
1 | Public Shared Function Compare ( strA As String, strB As String ) As Integer 比較兩個指定的字符串對象,并返回一個整數(shù),指示它們在排序順序中的相對位置。 |
2 | Public Shared Function Compare ( strA As String, strB As String, ignoreCase As Boolean ) As Integer 比較兩個指定的字符串對象,并返回一個整數(shù),指示它們在排序順序中的相對位置。 但是,如果布爾參數(shù)為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公共共享函數(shù)Concat(str0 As String,str1 As String,str2 As String,str3 As String)As String 連接四個字符串對象。 |
6 | Public Function Contains ( value As String ) As Boolean 返回一個值,指示指定的字符串對象是否出現(xiàn)在此字符串中。 |
7 | Public Shared Function Copy ( str As String ) As String 創(chuàng)建與指定字符串具有相同值的新String對象。 |
8 | pPublic Sub CopyTo ( sourceIndex As Integer, destination As Char(), destinationIndex As Integer, count As Integer ) 將指定數(shù)量的字符從字符串對象的指定位置復(fù)制到Unicode字符數(shù)組中的指定位置。 |
9 | Public Function EndsWith ( value As String ) As Boolean 確定字符串對象的結(jié)尾是否與指定的字符串匹配。 |
10 | Public Function Equals ( value As String ) As Boolean 確定當(dāng)前字符串對象,并指定字符串對象是否具有相同的值。 |
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 返回當(dāng)前字符串中指定Unicode字符第一次出現(xiàn)的從零開始的索引。 |
14 | Public Function IndexOf ( value As String ) As Integer 返回此實例中指定字符串第一次出現(xiàn)的從零開始的索引。 |
15 | Public Function IndexOf ( value As Char, startIndex As Integer ) As Integer 返回此字符串中指定Unicode字符第一次出現(xiàn)的從零開始的索引,從指定的字符位置開始搜索。 |
16 | Public Function IndexOf ( value As String, startIndex As Integer ) As Integer 返回此實例中指定字符串第一次出現(xiàn)的從零開始的索引,開始在指定的字符位置搜索。 |
17 | Public Function IndexOfAny ( anyOf As Char() ) As Integer 返回在指定的Unicode字符數(shù)組中的任何字符的此實例中第一次出現(xiàn)的從零開始的索引。 |
18 | Public Function IndexOfAny ( anyOf As Char(), startIndex As Integer ) As Integer 返回在指定的Unicode字符數(shù)組中的任意字符的此實例中第一次出現(xiàn)的從零開始的索引,開始在指定的字符位置搜索。 |
19 | Public Function Insert ( startIndex As Integer, value As String ) As String 返回一個新字符串,其中指定的字符串插入到當(dāng)前字符串對象中指定的索引位置。 |
20 | Public Shared Function IsNullOrEmpty ( value As String ) As Boolean 指示指定的字符串是空還是空字符串。 |
21 | Public Shared Function Join ( separator As String, ParamArray value As String() ) As String 連接字符串?dāng)?shù)組的所有元素,使用每個元素之間指定的分隔符。 |
22 | Public Shared Function Join ( separator As String, value As String(), startIndex As Integer, count As Integer ) As String 使用每個元素之間指定的分隔符連接字符串?dāng)?shù)組的指定元素。 |
23 | Public Function LastIndexOf ( value As Char ) As Integer 返回當(dāng)前字符串對象中指定Unicode字符的最后一次出現(xiàn)的從零開始的索引位置。 |
24 | Public Function LastIndexOf ( value As String ) As Integer 返回當(dāng)前字符串對象中指定字符串的最后一次出現(xiàn)的從零開始的索引位置。 |
25 | Public Function Remove ( startIndex As Integer )As String 刪除當(dāng)前實例中的所有字符,從指定位置開始,并繼續(xù)到最后一個位置,并返回字符串。 |
26 | Public Function Remove ( startIndex As Integer, count As Integer ) As String 從指定位置開始刪除當(dāng)前字符串中指定數(shù)量的字符,并返回字符串。 |
27 | Public Function Replace ( oldChar As Char, newChar As Char ) As String 用指定的Unicode字符替換當(dāng)前字符串對象中指定Unicode字符的所有出現(xiàn),并返回新字符串。 |
28 | Public Function Replace ( oldValue As String, newValue As String ) As String 將當(dāng)前字符串對象中指定字符串的所有出現(xiàn)替換為指定的字符串,并返回新字符串。 |
29 | Public Function Split ( ParamArray separator As Char() ) As String() 返回一個字符串?dāng)?shù)組,其中包含當(dāng)前字符串對象中的子字符串,由指定的Unicode字符數(shù)組的元素分隔。 |
30 | Public Function Split ( separator As Char(), count As Integer ) As String() 返回一個字符串?dāng)?shù)組,其中包含當(dāng)前字符串對象中的子字符串,由指定的Unicode字符數(shù)組的元素分隔。 int參數(shù)指定要返回的子字符串的最大數(shù)目。 |
31 | Public Function StartsWith ( value As String ) As Boolean 確定此字符串實例的開頭是否與指定的字符串匹配。 |
32 | Public Function ToCharArray As Char() 返回包含當(dāng)前字符串對象中所有字符的Unicode字符數(shù)組。 |
33 | Public Function ToCharArray ( startIndex As Integer, length As Integer ) As Char() 返回一個Unicode字符數(shù)組,其中包含當(dāng)前字符串對象中的所有字符,從指定的索引開始,直到指定的長度。 |
34 | Public Function ToLower As String 返回此字符串的轉(zhuǎn)換為小寫的副本。 |
35 | Public Function ToUpper As String 返回此字符串的轉(zhuǎn)換為大寫的副本。 |
36 | Public Function Trim As String 從當(dāng)前String對象中刪除所有前導(dǎo)和尾部空格字符。 |
上面列出的方法并不詳盡,請訪問MSDN庫獲取完整的方法列表和String類構(gòu)造函數(shù)。
下面的例子說明了上述一些方法:
比較字符串:
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
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
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
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
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
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
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
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
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
更多建議: