<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Visual Basic 中的字符串 > 原文: [https://zetcode.com/lang/visualbasic/strings/](https://zetcode.com/lang/visualbasic/strings/) 在 Visual Basic 教程的這一部分中,我們將更詳細地處理字符串數據。 字符串是計算機語言中最重要的數據類型。 這就是為什么我們將一整章專門討論在 Visual Basic 中使用字符串的原因。 ## 第一個例子 字符串字面值是表示計算機程序文本內的字符串值的符號。 在 Visual Basic 中,字符串字面值用雙引號引起來。 Visual Basic 中的字符串是 Unicode 字符序列。 ```vb Option Strict On Module Example Sub Main() Dim str1 As String = "There are 10" Dim str2 As String = " apples" Console.WriteLine(str1 + str2) Console.WriteLine("The length of the first string is " _ + str1.Length.ToString() + " characters") End Sub End Module ``` 在前面的示例中,我們創建了兩個字符串變量。 然后我們將它們相加并計算第一個字符串的長度。 ```vb Dim str1 As String = "There are 10" ``` 聲明并初始化一個字符串變量。 ```vb Console.WriteLine(str1 + str2) ``` 兩個字符串連接在一起。 我們使用`+`運算符添加兩個字符串。 ```vb Console.WriteLine("The length of the first string is " _ + str1.Length.ToString() + " characters") ``` `Length`屬性用于確定字符串的長度。 ```vb $ ./basics.exe There are 10 apples The length of the first string is 12 characters ``` 運行示例可得出此結果。 ## 使用引號 雙引號用于在 Visual Basic 中創建字符串字面值。 如果我們想顯示報價,例如直接講話怎么辦? 要打印雙引號,必須在其前面加上另一個雙引號。 ```vb Option Strict On Module Example Sub Main() Console.WriteLine("There are many stars.") Console.WriteLine("He said, ""Which one is your favourite?""") End Sub End Module ``` 在控制臺上打印雙引號時,必須在其前面加上另一個雙引號。 ```vb Console.WriteLine("He said, ""Which one is your favourite?""") ``` 在這里,我們展示了如何將直接語音打印到控制臺。 如果我們不使用兩個雙引號,則會誤導編譯器。 它會看到兩個連續的字符串。 ```vb $ ./quotes.exe There are many stars. He said, "Which one is your favourite?" ``` 輸出。 ## 多行字符串 可以在 Visual Basic 中創建多行字符串。 ```vb Option Strict On Module Example Sub Main() Dim multiString As String = "I cheated myself" + vbNewLine + _ "like I knew I would" + vbNewLine + _ "I told ya, I was trouble" + vbNewLine + _ "you know that I'm no good" Console.WriteLine(multiString) End Sub End Module ``` 該示例創建一個跨越多行的字符串。 我們使用換行符,加號和`vbNewLine`顯示常量。 ```vb $ ./multiline.exe I cheated myself like I knew I would I told ya, I was trouble you know that I'm no good ``` 此文本以四行顯示。 所有文本都分配給一個字符串變量。 ## 比較字符串 ```vb Option Strict On Module Example Sub Main() Console.WriteLine("12" = "12") 'Returns True Console.WriteLine("17" < "9") ' Returns True Console.WriteLine("aa" > "ab") ' Returns False End Sub End Module ``` 比較運算符在字符串上下文中的工作方式不同。 ```vb Console.WriteLine("17" < "9") 'Returns True ``` 值 17 不小于 9。但是在兩個字符串上應用`<`時,我們不比較數字。 我們比較字符的排序順序。 1 在 9 之前,因此具有“較低位置”,并且比較返回`True`。 ```vb Console.WriteLine("aa" > "ab") ' Returns False ``` 如果前兩個字符相等,則繼續對后兩個字符進行操作。 a 字符位于 b 之前,并且比較操作返回`False`。 有`String.Compare()`方法,該方法比較兩個指定的字符串并返回一個整數,該整數指示它們在排序順序中的相對位置。 如果返回的值小于零,則第一個字符串小于第二個字符串。 如果返回零,則兩個字符串相等。 最后,如果返回的值大于零,則第一個字符串大于第二個字符串。 ```vb Option Strict On Module Example Sub Main() Dim str1 As String = "Visual Basic" Dim str2 As String = "visual basic" Console.WriteLine(String.Compare(str1, str2, True)) Console.WriteLine(String.Compare(str1, str2, False)) End Sub End Module ``` 第三個可選的`ignoreCase`參數確定是否應履行該案件。 ```vb Console.WriteLine(String.Compare(str1, str2, True)) ``` 比較兩個字符串并忽略大小寫。 此行將 0 打印到控制臺。 有一個`Like`運算符,可用于簡單的正則表達式匹配。 ```vb Option Strict On Module Example Dim words() As String = {"Seven", "even", "Maven", "Amen", "Leven"} Sub Main() For Each word As String In words If word Like "?*even" Then Console.WriteLine("{0} matches the pattern", word) Else Console.WriteLine("{0} does not match the pattern", word) End If Next End Sub End Module ``` 我們有很多單詞。 我們將針對正則表達式模式測試這些單詞。 如果單詞匹配或不匹配,我們將向控制臺打印一條消息。 ```vb Dim words() As String = {"Seven", "even", "Maven", "Amen", "Leven"} ``` 這是五個單詞的數組。 ```vb For Each word As String In words ... Next ``` 我們使用`For Each`循環遍歷數組。 當前單詞存儲在單詞變量中。 ```vb If word Like ".*even" Then Console.WriteLine("{0} matches the pattern", word) Else Console.WriteLine("{0} does not match the pattern", word) End If ``` `"?*even"`是一個簡單的正則表達式模式。 `?`匹配任何單個字符,`*`零個或多個字符。 我們打印一條消息以通知單詞是否與模式匹配。 ## 字符串函數 Visual Basic 具有有用的內置函數,可用于處理字符串。 ```vb Option Strict On Module Example Sub Main() Dim str As String = "Visual Basic" Dim n As Integer = Len(str) Dim l As String = Left(str, 6) Dim r As String = Right(str, 5) Dim repl As String = Replace(str, "Basic", "form") Console.WriteLine("The string has {0} characters", n) Console.WriteLine("The Left function returns {0}", l) Console.WriteLine("The Right function returs {0}", r) Console.WriteLine("The Replace function returns {0}", repl) End Sub End Module ``` 我們在 Visual Basic 中引入了四個字符串函數。 ```vb Dim n As Integer = Len(str) ``` `Len()`函數返回字符串中的字符數。 ```vb Dim l As String = Left(str, 6) ``` 此`Left()`函數的調用從字符串的左側返回 6 個字符。 在我們的例子中,`Visual`。 ```vb Dim r As String = Right(str, 5) ``` 在這里,我們從右邊得到 5 個字符。 ```vb Dim repl As String = Replace(str, "Basic", "form") ``` 字符串在 Visual Basic 中是不可變的。 當我們使用`Replace()`函數時,我們返回一個新的修改后的字符串,其中第一個字符串替換為第二個字符串。 ```vb $ ./strfunc.exe The string has 12 characters The Left function returns Visual The Right function returs Basic The Replace function returns Visual form ``` 運行該示例將得出前面的結果。 `Join()`和`Split()`函數非常方便。 ```vb Option Strict On Imports System Module Example Sub Main() Dim items() As String = {"C#", "Visual Basic", "Java", "Perl"} Dim langs As String = Join(items, ",") Console.WriteLine(langs) Dim ls() As String = Split(langs, ",") For Each lang As String In ls Console.WriteLine(lang) Next End Sub End Module ``` 在我們的程序中,我們將使用這兩個函數來連接和分割字符串。 ```vb Dim langs As String = Join(items, ",") ``` 數組中的所有單詞都被加入。 我們從中構建一個字符串。 每兩個字之間會有一個逗號。 ```vb Dim ls() As String = Split(langs, ",") ``` 作為反向操作,我們分割了`langs`字符串。 `Split()`函數返回一個由字符分隔的單詞數組。 在我們的情況下,它是一個逗號字符。 ```vb For Each lang As String In ls Console.WriteLine(lang) Next ``` 我們遍歷數組并打印其元素。 ```vb $ ./joinsplit.exe C#,Visual Basic,Java,Perl C# Visual Basic Java Perl ``` 示例的輸出。 ## 字符串方法 除了字符串函數,還有幾種字符串方法。 其中一些提供相同的功能。 正如我們已經提到的,字符串不是原始數據類型。 它們是引用類型。 它們是對象,這些對象具有可以完成某些工作的方法。 ```vb Option Strict On Imports System Module Example Sub Main() Dim str As String = "Determination" Console.WriteLine(str.Contains("e")) Console.WriteLine(str.IndexOf("e")) Console.WriteLine(str.LastIndexOf("i")) Console.WriteLine(str.ToUpper) Console.WriteLine(str.ToLower) End Sub End Module ``` 在上面的示例中,我們介紹了五個字符串方法。 ```vb Console.WriteLine(str.Contains("e")) ``` 如果字符串包含特定字符,則`Contains()`方法返回`True`。 ```vb Console.WriteLine(str.IndexOf("e")) ``` `IndexOf`返回字符串中字母的第一個索引。 ```vb Console.WriteLine(str.LastIndexOf("i")) ``` `LastIndexOf()`方法返回字符串中字母的最后一個索引。 ```vb Console.WriteLine(str.ToUpper) Console.WriteLine(str.ToLower) ``` 字符串的字母通過`ToUpper`方法轉換為大寫,并通過`ToLower`方法轉換為小寫。 ```vb $ ./strmethods.exe True 1 10 DETERMINATION determination ``` 運行程序。 ## 復制與克隆 我們將描述兩種方法之間的區別。 復制并克隆。 `Copy()`方法創建一個新的`String`實例,該實例的值與指定的`String`相同。 `Clone()`方法返回對正在克隆的字符串的引用。 它不是堆上字符串的獨立副本。 它是同一字符串上的另一個引用。 ```vb Option Strict On Module Example Sub Main() Dim str As String = "Visual Basic" Dim cloned As String = CType(str.Clone(), String) Dim copied As String = String.Copy(str) Console.WriteLine(str = cloned) ' Prints True Console.WriteLine(str = copied) ' Prints True Console.WriteLine(str Is cloned) ' Prints True Console.WriteLine(str Is copied) ' Prints False End Sub End Module ``` 我們的示例演示了兩種方法之間的區別。 ```vb Dim cloned As String = CType(str.Clone(), String) Dim copied As String = String.Copy(str) ``` 字符串值被克隆并復制。 ```vb Console.WriteLine(str = cloned) ' Prints True Console.WriteLine(str = copied) ' Prints True ``` 所有三個字符串的內容都是相同的。 ```vb Console.WriteLine(str Is cloned) ' Prints True Console.WriteLine(str Is copied) ' Prints False ``` `Is`運算符比較兩個引用對象。 因此,將復制的字符串與原始字符串進行比較將返回`False`。 因為它們是兩個不同的對象。 ## 格式化字符串 在下面的示例中,我們將格式化字符串。 .NET Framework 具有稱為復合格式的功能。 `Format()`和`WriteLine()`方法支持它。 方法采用對象列表和復合格式字符串作為輸入。 格式字符串由固定字符串加上一些格式項組成。 這些格式項是與列表中的對象相對應的索引占位符。 格式項具有以下語法: ```vb {index[,length][:formatString]} ``` 索引組件是必需的。 它是一個從 0 開始的數字,表示對象列表中的一項。 多個項目可以引用對象列表的同一元素。 如果格式項未引用該對象,則將其忽略。 如果我們在對象列表的范圍之外引用,則會拋出運行時異常。 長度部分是可選的。 它是參數的字符串表示形式中的最小字符數。 如果為正,則該參數為右對齊;否則為 0。 如果為負,則為左對齊。 如果指定,則必須用冒號分隔索引和長度。 `formatString`是可選的。 它是一個格式化值的字符串,是一種特定的方式。 它可以用來格式化日期,時間,數字或枚舉。 在這里,我們展示了如何使用格式項的長度分量。 我們將三列數字打印到終端。 左,中和右對齊。 ```vb Option Strict On Imports System Module Example Dim oranges As Byte = 2 Dim apples As Byte = 4 Dim bananas As Byte = 3 Sub Main() Dim str1 As String = "There are {0} oranges, {1} apples and " + _ "{2} bananas" Dim str2 As String = "There are {1} oranges, {2} bananas and " + _ "{0} apples" Console.WriteLine(str1, oranges, apples, bananas) Console.WriteLine(str2, apples, oranges, bananas) End Sub End Module ``` 我們向控制臺打印一條簡單的消息。 我們僅使用格式項的索引部分。 ```vb Dim str1 As String = "There are {0} oranges, {1} apples and " + _ "{2} bananas" ``` {0},{1}和{2}是格式項。 我們指定索引組件。 其他組件是可選的。 ```vb Console.WriteLine(str1, oranges, apples, bananas) ``` 現在,我們將復合格式放在一起。 我們有字符串和對象列表(橙色,蘋果,香蕉)。 `{0}`格式項目是指橙色。 `WriteLine()`方法將`{0}`格式項替換為`oranges`變量的內容。 ```vb Dim str2 As String = "There are {1} oranges, {2} bananas and " + _ "{0} apples" ``` 引用對象的格式項的順序很重要。 ```vb $ ./format1.exe There are 2 oranges, 4 apples and 3 bananas There are 2 oranges, 3 bananas and 4 apples ``` ```vb Option Strict On Module Example Sub Main() Console.WriteLine("{0} {1, 12}", _ "Decimal", "Hexadecimal") Console.WriteLine("{0:D} {1,8:X}", _ 502, 546) Console.WriteLine("{0:D} {1,8:X}", _ 345, 765) Console.WriteLine("{0:D} {1,8:X}", _ 320, 654) Console.WriteLine("{0:D} {1,8:X}", _ 120, 834) Console.WriteLine("{0:D} {1,8:X}", _ 620, 454) End Sub End Module ``` 我們以十進制和十六進制格式打印數字。 我們還使用長度分量對齊數字。 ```vb Console.WriteLine("{0:D} {1,8:X}", _ 502, 546) ``` `{0:D}`格式項指定,將采用提供的對象列表中的第一項并將其格式化為十進制格式。 `{1,8:X}`格式項目取第二項。 將其格式化為十六進制格式`(:X)`。 字符串長度為 8 個字符`,8`。 因為數字只有三個字符,所以它會右對齊并用空字符串填充。 ```vb $ ./format2.exe Decimal Hexadecimal 502 222 345 2FD 320 28E 120 342 620 1C6 ``` 運行示例。 最后兩個示例將格式化數字和日期數據。 ```vb Option Strict On Module Example Sub Main() Console.WriteLine(String.Format("Number: {0:N}", 126)) Console.WriteLine(String.Format("Scientific: {0:E}", 126)) Console.WriteLine(String.Format("Currency: {0:C}", 126)) Console.WriteLine(String.Format("Percent: {0:P}", 126)) Console.WriteLine(String.Format("Hexadecimal: {0:X}", 126)) End Sub End Module ``` 該示例演示了數字的標準格式說明符。 數字 126 以五種不同的格式打印。 正常,科學,貨幣,百分比和十六進制。 ```vb $ ./format3.exe Number: 126.00 Scientific: 1.260000E+002 Currency: $126.00 Percent: 12,600.00 % Hexadecimal: 7E ``` 輸出。 最后,我們將格式化日期和時間數據。 ```vb Option Strict On Module Example Sub Main() Dim today As DateTime = DateTime.Now() Console.WriteLine(String.Format("Short date: {0:d}", today)) Console.WriteLine(String.Format("Login date: {0:D}", today)) Console.WriteLine(String.Format("Short time: {0:t}", today)) Console.WriteLine(String.Format("Long time: {0:T}", today)) Console.WriteLine(String.Format("Month: {0:M}", today)) Console.WriteLine(String.Format("Year: {0:Y}", today)) End Sub End Module ``` 前面的示例演示了日期的標準格式說明符。 ```vb $ ./format4.exe Short date: 8/18/2010 Login date: Wednesday, August 18, 2010 Short time: 11:29 PM Long time: 11:29:40 PM Month: August 18 Year: August, 2010 ``` Output. Visual Basic 教程的這一部分介紹了字符串。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看