<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國際加速解決方案。 廣告
                # 如何:比較字符串(C# 編程指南) 比較字符串時,產生的結果會是一個字符串大于或小于另一個字符串,或者兩個字符串相等。根據執行的是序號比較還是區分區域性比較,確定結果時所依據的規則會有所不同。對特定的任務使用正確類型的比較十分重要。 當需要對兩個字符串的值進行比較和排序而不需要考慮語言慣例時,請使用基本的序號比較。基本的序號比較 (**System.StringComparison.Ordinal**) 是區分大小寫的,這意味著兩個字符串的字符必須完全匹配:“and”不等于“And”或“AND”。常用的變量有 **System.StringComparison.OrdinalIgnoreCase**,它將匹配“and”、“And”和“AND”;還有 **StringComparison.OrdinalIgnoreCase**,它常用于比較文件名、路徑名和網絡路徑,以及其值不隨用戶計算機的區域設置的更改而變化的任何其他字符串。有關更多信息,請參見[System.StringComparison](https://msdn.microsoft.com/zh-cn/library/system.stringcomparison.aspx)。 區分區域性比較通常用于對終端用戶輸入的字符串進行比較和排序,因為這些字符串的字符和排序約定可能會根據用戶計算機的區域設置的不同而有所不同。即使是包含相同字符的字符串,也可能會根據當前線程的區域性不同而不同。 | ![](https://box.kancloud.cn/2016-01-31_56adb62c1380a.jpg) 注意 | | :-- | | 在比較字符串時,您使用的方法應該顯式指定了要執行的比較類型。這可增強代碼的可維護性和可讀性。應盡可能使用具有 [StringComparison](https://msdn.microsoft.com/zh-cn/library/system.stringcomparison.aspx) 枚舉參數的 [System.String](https://msdn.microsoft.com/zh-cn/library/system.string.aspx) 和 [System.Array](https://msdn.microsoft.com/zh-cn/library/system.array.aspx) 類的方法重載,以便可以指定要執行的比較類型。比較字符串時,最好避免使用 **==** 和 **!=** 運算符。還應該避免使用 [String.CompareTo](https://msdn.microsoft.com/zh-cn/library/system.string.compareto.aspx) 實例方法,因為這些重載沒有 [StringComparison](https://msdn.microsoft.com/zh-cn/library/system.stringcomparison.aspx)。 | 下面的示例演示如何正確地比較其值將不隨用戶計算機的區域設置的改變而變化的字符串。此外,它還演示 C# 中的字符串限定功能。如果程序聲明了兩個或更多個相同的字符串變量,編譯器會將它們存儲在同一位置。通過調用 [ReferenceEquals](https://msdn.microsoft.com/zh-cn/library/system.object.referenceequals.aspx) 方法,可以看到這兩個字符串實際引用內存中的同一對象。使用 [String.Copy](https://msdn.microsoft.com/zh-cn/library/system.string.copy.aspx) 方法可避免此限定,如下面的示例所示。 ``` // Internal strings that will never be localized. string root = @"C:\users"; string root2 = @"C:\Users"; // Use the overload of the Equals method that specifies a StringComparison. // Ordinal is the fastest way to compare two strings. bool result = root.Equals(root2, StringComparison.Ordinal); Console.WriteLine("Ordinal comparison: {0} and {1} are {2}", root, root2, result ? "equal." : "not equal."); // To ignore case means "user" equals "User". This is the same as using // String.ToUpperInvariant on each string and then performing an ordinal comparison. result = root.Equals(root2, StringComparison.OrdinalIgnoreCase); Console.WriteLine("Ordinal ignore case: {0} and {1} are {2}", root, root2, result ? "equal." : "not equal."); // A static method is also available. bool areEqual = String.Equals(root, root2, StringComparison.Ordinal); // String interning. Are these really two distinct objects? string a = "The computer ate my source code."; string b = "The computer ate my source code."; // ReferenceEquals returns true if both objects // point to the same location in memory. if (String.ReferenceEquals(a, b)) Console.WriteLine("a and b are interned."); else Console.WriteLine("a and b are not interned."); // Use String.Copy method to avoid interning. string c = String.Copy(a); if (String.ReferenceEquals(a, c)) Console.WriteLine("a and c are interned."); else Console.WriteLine("a and c are not interned."); // Output: // Ordinal comparison: C:\users and C:\Users are not equal. // Ordinal ignore case: C:\users and C:\Users are equal. // a and b are interned. // a and c are not interned. ``` 下面的示例演示如何通過首選方式比較字符串,該首選方式使用具有 [StringComparison](https://msdn.microsoft.com/zh-cn/library/system.stringcomparison.aspx) 枚舉的 [System.String](https://msdn.microsoft.com/zh-cn/library/system.string.aspx) 方法。請注意,這里沒有使用 [String.CompareTo](https://msdn.microsoft.com/zh-cn/library/system.string.compareto.aspx) 實例方法,因為這些重載沒有 [StringComparison](https://msdn.microsoft.com/zh-cn/library/system.stringcomparison.aspx)。 ``` // "They dance in the street." // Linguistically (in Windows), "ss" is equal to // the German essetz: '?' character in both en-US and de-DE cultures. string first = "Sie tanzen in die Stra?e."; string second = "Sie tanzen in die Strasse."; Console.WriteLine("First sentence is {0}", first); Console.WriteLine("Second sentence is {0}", second); // Store CultureInfo for the current culture. Note that the original culture // can be set and retrieved on the current thread object. System.Threading.Thread thread = System.Threading.Thread.CurrentThread; System.Globalization.CultureInfo originalCulture = thread.CurrentCulture; // Set the culture to en-US. thread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); // For culture-sensitive comparisons, use the String.Compare // overload that takes a StringComparison value. int i = String.Compare(first, second, StringComparison.CurrentCulture); Console.WriteLine("Comparing in {0} returns {1}.", originalCulture.Name, i); // Change the current culture to Deutch-Deutchland. thread.CurrentCulture = new System.Globalization.CultureInfo("de-DE"); i = String.Compare(first, second, StringComparison.CurrentCulture); Console.WriteLine("Comparing in {0} returns {1}.", thread.CurrentCulture.Name, i); // For culture-sensitive string equality, use either StringCompare as above // or the String.Equals overload that takes a StringComparison value. thread.CurrentCulture = originalCulture; bool b = String.Equals(first, second, StringComparison.CurrentCulture); Console.WriteLine("The two strings {0} equal.", b == true ? "are" : "are not"); /* * Output: First sentence is Sie tanzen in die Stra?e. Second sentence is Sie tanzen in die Strasse. Comparing in en-US returns 0. Comparing in de-DE returns 0. The two strings are equal. */ ``` 下面的示例演示如何使用具有 [System.StringComparer](https://msdn.microsoft.com/zh-cn/library/system.stringcomparer.aspx) 參數的靜態 [Array](https://msdn.microsoft.com/zh-cn/library/system.array.aspx) 方法以區分區域性的方式對數組中的字符串進行排序和搜索。 ``` class SortStringArrays { static void Main() { string[] lines = new string[] { @"c:\public\textfile.txt", @"c:\public\textFile.TXT", @"c:\public\Text.txt", @"c:\public\testfile2.txt" }; Console.WriteLine("Non-sorted order:"); foreach (string s in lines) { Console.WriteLine(" {0}", s); } Console.WriteLine("\n\rSorted order:"); // Specify Ordinal to demonstrate the different behavior. Array.Sort(lines, StringComparer.Ordinal); foreach (string s in lines) { Console.WriteLine(" {0}", s); } string searchString = @"c:\public\TEXTFILE.TXT"; Console.WriteLine("Binary search for {0}", searchString); int result = Array.BinarySearch(lines, searchString, StringComparer.OrdinalIgnoreCase); ShowWhere<string>(lines, result); //Console.WriteLine("{0} {1}", result > 0 ? "Found" : "Did not find", searchString); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } // Displays where the string was found, or, if not found, // where it would have been located. private static void ShowWhere<T>(T[] array, int index) { if (index < 0) { // If the index is negative, it represents the bitwise // complement of the next larger element in the array. index = ~index; Console.Write("Not found. Sorts between: "); if (index == 0) Console.Write("beginning of array and "); else Console.Write("{0} and ", array[index - 1]); if (index == array.Length) Console.WriteLine("end of array."); else Console.WriteLine("{0}.", array[index]); } else { Console.WriteLine("Found at index {0}.", index); } } } /* * Output: Non-sorted order: c:\public\textfile.txt c:\public\textFile.TXT c:\public\Text.txt c:\public\testfile2.txt Sorted order: c:\public\Text.txt c:\public\testfile2.txt c:\public\textFile.TXT c:\public\textfile.txt Binary search for c:\public\TEXTFILE.TXT Found at index 2. */ ``` 當元素或鍵的類型為 **string** 時,[System.Collections.Hashtable](https://msdn.microsoft.com/zh-cn/library/system.collections.hashtable.aspx)、[System.Collections.Generic.Dictionary&lt;TKey, TValue&gt;](https://msdn.microsoft.com/zh-cn/library/xfhwa508.aspx) 和 [System.Collections.Generic.List&lt;T&gt;](https://msdn.microsoft.com/zh-cn/library/6sh2ey19.aspx) 等集合類的構造函數具有 [System.StringComparer](https://msdn.microsoft.com/zh-cn/library/system.stringcomparer.aspx) 參數。通常,應盡可能使用這些構造函數,并指定 Ordinal 或 OrdinalIgnoreCase。 ## 請參閱 [System.Globalization.CultureInfo](https://msdn.microsoft.com/zh-cn/library/system.globalization.cultureinfo.aspx) [System.StringComparer](https://msdn.microsoft.com/zh-cn/library/system.stringcomparer.aspx) [字符串(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/ms228362.aspx) [在 .NET Framework 中比較字符串](https://msdn.microsoft.com/zh-cn/library/fbh501kz.aspx) [對應用程序進行全球化和本地化](https://msdn.microsoft.com/zh-cn/library/1021kkz0.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>

                              哎呀哎呀视频在线观看