<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 如何:串聯多個字符串(C# 編程指南) 串聯是將一個字符串追加到另一個字符串末尾的過程。使用 **+** 運算符串聯字符串文本或字符串常量時,編譯器會創建一個字符串。串聯不在運行時發生。但字符串變量只能在運行時串聯,對此,您應該了解各種方法的性能含義。 下面的示例演示如何將一個長字符串拆分為幾個較短的字符串,從而提高源代碼的可讀性。這些較短的字符串將在編譯時串聯成一個字符串。無論涉及到多少個字符串,都不會有運行時性能開銷。 ``` static void Main() { // Concatenation of literals is performed at compile time, not run time. string text = "Historically, the world of data and the world of objects " + "have not been well integrated. Programmers work in C# or Visual Basic " + "and also in SQL or XQuery. On the one side are concepts such as classes, " + "objects, fields, inheritance, and .NET Framework APIs. On the other side " + "are tables, columns, rows, nodes, and separate languages for dealing with " + "them. Data types often require translation between the two worlds; there are " + "different standard functions. Because the object world has no notion of query, a " + "query can only be represented as a string without compile-time type checking or " + "IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to " + "objects in memory is often tedious and error-prone."; Console.WriteLine(text); } ``` 若要串聯字符串變量,可以使用 **+** 或 **+=** 運算符,也可以使用 [String.Concat](https://msdn.microsoft.com/zh-cn/library/system.string.concat.aspx)、[String.Format](https://msdn.microsoft.com/zh-cn/library/system.string.format.aspx) 或 [StringBuilder.Append](https://msdn.microsoft.com/zh-cn/library/system.text.stringbuilder.append.aspx) 方法。 **+** 運算符容易使用,且有利于提高代碼的直觀性。即使在一條語句中使用多個 + 運算符,字符串內容也將只復制一次。但是,如果重復此操作多次(如使用循環),則可能會導致出現效率問題。例如,考慮下面的代碼: ``` static void Main(string[] args) { // To run this program, provide a command line string. // In Visual Studio, see Project > Properties > Debug. string userName = args[0]; string date = DateTime.Today.ToShortDateString(); // Use the + and += operators for one-time concatenations. string str = "Hello " + userName + ". Today is " + date + "."; System.Console.WriteLine(str); str += " How are you today?"; System.Console.WriteLine(str); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } // Example output: // Hello Alexander. Today is 1/22/2008. // Hello Alexander. Today is 1/22/2008\. How are you today? // Press any key to exit. // ``` | ![](https://box.kancloud.cn/2016-01-31_56adb62c1380a.jpg) 注意 | | :-- | | 在字符串串聯操作中,C# 編譯器對 null 字符串和空字符串進行相同的處理,但它不轉換原始 null 字符串的值。 | 如果您串聯的字符串數量不那么巨大(例如,在循環中),那么這些代碼的性能成本可能不會很高。上述情況同樣適用于 [String.Concat](https://msdn.microsoft.com/zh-cn/library/system.string.concat.aspx) 和 [String.Format](https://msdn.microsoft.com/zh-cn/library/system.string.format.aspx) 方法。 但如果性能的優劣很重要,則應該總是使用 [StringBuilder](https://msdn.microsoft.com/zh-cn/library/system.text.stringbuilder.aspx) 類來串聯字符串。下面的代碼使用 [StringBuilder](https://msdn.microsoft.com/zh-cn/library/system.text.stringbuilder.aspx) 類的 [Append](https://msdn.microsoft.com/zh-cn/library/system.text.stringbuilder.append.aspx) 方法來串聯字符串,因此不會有 **+** 運算符的鏈接作用產生。 ``` class StringBuilderTest { static void Main() { string text = null; // Use StringBuilder for concatenation in tight loops. System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < 100; i++) { sb.AppendLine(i.ToString()); } System.Console.WriteLine(sb.ToString()); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } // Output: // 0 // 1 // 2 // 3 // 4 // ... // ``` ## 請參閱 [String](https://msdn.microsoft.com/zh-cn/library/system.string.aspx) [StringBuilder](https://msdn.microsoft.com/zh-cn/library/system.text.stringbuilder.aspx) [C# 編程指南](https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx) [字符串(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/ms228362.aspx)
                  <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>

                              哎呀哎呀视频在线观看