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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 如何:修改字符串內容(C# 編程指南) 由于字符串是不可變的,因此一個字符串對象一旦創建,值就不能再更改(在不使用不安全代碼的情況下)。不過,修改字符串的值然后將結果存儲到新的字符串對象中有很多種方法。 [System.String](https://msdn.microsoft.com/zh-cn/library/system.string.aspx) 類提供作用于輸入字符串并返回新字符串對象的方法。在很多情況下,可以將這個新對象賦給保存原始字符串的變量。 [System.Text.RegularExpressions.Regex](https://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex.aspx) 類提供其他一些以類似方式工作的方法。 [System.Text.StringBuilder](https://msdn.microsoft.com/zh-cn/library/system.text.stringbuilder.aspx) 類提供一個可“就地”修改的字符緩沖區。調用 [StringBuilder.ToString](https://msdn.microsoft.com/zh-cn/library/4b1063zt.aspx) 方法可新建包含此緩沖區的當前內容的字符串對象。 下面的示例演示替換或移除指定字符串中的子字符串的各種方法。 ``` class ReplaceSubstrings { string searchFor; string replaceWith; static void Main(string[] args) { ReplaceSubstrings app = new ReplaceSubstrings(); string s = "The mountains are behind the clouds today."; // Replace one substring with another with String.Replace. // Only exact matches are supported. s = s.Replace("mountains", "peaks"); Console.WriteLine(s); // Output: The peaks are behind the clouds today. // Use Regex.Replace for more flexibility. // Replace "the" or "The" with "many" or "Many". // using System.Text.RegularExpressions app.searchFor = "the"; // A very simple regular expression. app.replaceWith = "many"; s = Regex.Replace(s, app.searchFor, app.ReplaceMatchCase, RegexOptions.IgnoreCase); Console.WriteLine(s); // Output: Many peaks are behind many clouds today. // Replace all occurrences of one char with another. s = s.Replace(' ', '_'); Console.WriteLine(s); // Output: Many_peaks_are_behind_many_clouds_today. // Remove a substring from the middle of the string. string temp = "many_"; int i = s.IndexOf(temp); if (i >= 0) { s = s.Remove(i, temp.Length); } Console.WriteLine(s); // Output: Many_peaks_are_behind_clouds_today. // Remove trailing and leading whitespace. // See also the TrimStart and TrimEnd methods. string s2 = " I'm wider than I need to be. "; // Store the results in a new string variable. temp = s2.Trim(); Console.WriteLine(temp); // Output: I'm wider than I need to be. // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit"); Console.ReadKey(); } // Custom match method called by Regex.Replace // using System.Text.RegularExpressions string ReplaceMatchCase(Match m) { // Test whether the match is capitalized if (Char.IsUpper(m.Value[0]) == true) { // Capitalize the replacement string // using System.Text; StringBuilder sb = new StringBuilder(replaceWith); sb[0] = (Char.ToUpper(sb[0])); return sb.ToString(); } else { return replaceWith; } } } ``` 若要使用數組表示法訪問字符串中的各個字符,可以使用 [StringBuilder](https://msdn.microsoft.com/zh-cn/library/system.text.stringbuilder.aspx) 對象,該對象重載 **[]** 運算符以提供對其內部字符緩沖區的訪問。也可以使用 [ToCharArray](https://msdn.microsoft.com/zh-cn/library/ezftk57x.aspx) 方法將該字符串轉換為一個字符數組。下面的示例使用 **ToCharArray** 創建該數組。然后修改該數組中的某些元素。之后再調用采用一個字符數組作為輸入參數的字符串構造函數來創建一個新字符串。 ``` class ModifyStrings { static void Main() { string str = "The quick brown fox jumped over the fence"; System.Console.WriteLine(str); char[] chars = str.ToCharArray(); int animalIndex = str.IndexOf("fox"); if (animalIndex != -1) { chars[animalIndex++] = 'c'; chars[animalIndex++] = 'a'; chars[animalIndex] = 't'; } string str2 = new string(chars); System.Console.WriteLine(str2); // Keep the console window open in debug mode System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } /* Output: The quick brown fox jumped over the fence The quick brown cat jumped over the fence */ ``` 下面的示例針對的是一種非常罕見的情況,即您可能希望使用不安全代碼以類似于 C 樣式字符數組的方式就地修改字符串。此示例演示如何使用 fixed 關鍵字“就地”訪問各個字符。此外還演示對字符串進行不安全操作可能產生的一個副作用,此副作用是由于 C# 編譯器在內部存儲(暫存)字符串的方式而導致的。通常,除非絕對必要,否則不應該使用這種方法。 ``` class UnsafeString { unsafe static void Main(string[] args) { // Compiler will store (intern) // these strings in same location. string s1 = "Hello"; string s2 = "Hello"; // Change one string using unsafe code. fixed (char* p = s1) { p[0] = 'C'; } // Both strings have changed. Console.WriteLine(s1); Console.WriteLine(s2); // Keep console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } ``` ## 請參閱 [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>

                              哎呀哎呀视频在线观看