|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | + |
| 4 | +public class CompareOptionsExample |
| 5 | +{ |
| 6 | + public static void Run() |
| 7 | + { |
| 8 | + // Uppercase and lowercase characters are equivalent (according to the culture rules) |
| 9 | + // when IgnoreCase is used. |
| 10 | + TestStringEquality("ONE two", "one TWO", "Case sensitivity", CompareOptions.IgnoreCase); |
| 11 | + |
| 12 | + // Punctuation is ignored with the IgnoreSymbols option. |
| 13 | + TestStringEquality("hello world", "hello, world!", "Punctuation", CompareOptions.IgnoreSymbols); |
| 14 | + |
| 15 | + // Whitespace and mathematical symbols are also ignored with IgnoreSymbols. |
| 16 | + TestStringEquality("3 + 5 = 8", "358", "Whitespace and mathematical symbols", CompareOptions.IgnoreSymbols); |
| 17 | + |
| 18 | + // Caution: currency symbols and thousands separators are ignored with IgnoreSymbols. |
| 19 | + // Parse strings containing numbers/currency and compare them numerically instead. |
| 20 | + TestStringEquality("Total $15,000", "Total: £150.00", "Currency symbols, decimals and thousands separators", CompareOptions.IgnoreSymbols); |
| 21 | + |
| 22 | + // Full width characters are common in East Asian languages. Use the IgnoreWidth |
| 23 | + // option to treat full- and half-width characters as equal. |
| 24 | + TestStringEquality("abc,-", "abc,-", "Half width and full width characters", CompareOptions.IgnoreWidth); |
| 25 | + |
| 26 | + // The same string in Hiragana and Katakana is equal when IgnoreKanaType is used. |
| 27 | + TestStringEquality("ありがとう", "アリガトウ", "Hiragana and Katakana strings", CompareOptions.IgnoreKanaType); |
| 28 | + |
| 29 | + // When comparing with the IgnoreNonSpace option, characters like diacritical marks are ignored. |
| 30 | + TestStringEquality("café", "cafe", "Diacritical marks", CompareOptions.IgnoreNonSpace); |
| 31 | + |
| 32 | + // Ligature characters and their non-ligature forms compare equal with the IgnoreNonSpace option. |
| 33 | + // Note: prior to .NET 5, ligature characters were equal to their expanded forms by default. |
| 34 | + TestStringEquality("straße œuvre cæsar", "strasse oeuvre caesar", "Ligature characters", CompareOptions.IgnoreNonSpace); |
| 35 | + } |
| 36 | + |
| 37 | + private static void TestStringEquality(string str1, string str2, string description, CompareOptions options) |
| 38 | + { |
| 39 | + Console.WriteLine(Environment.NewLine + description + ":"); |
| 40 | + // First test with the default CompareOptions then with the provided options |
| 41 | + TestStringEquality(str1, str2, CompareOptions.None); |
| 42 | + TestStringEquality(str1, str2, options); |
| 43 | + } |
| 44 | + |
| 45 | + private static void TestStringEquality(string str1, string str2, CompareOptions options) |
| 46 | + { |
| 47 | + Console.Write($" When using CompareOptions.{options}, \"{str1}\" and \"{str2}\" are "); |
| 48 | + if (string.Compare(str1, str2, CultureInfo.InvariantCulture, options) != 0) |
| 49 | + { |
| 50 | + Console.Write("not "); |
| 51 | + } |
| 52 | + Console.WriteLine("equal."); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/* |
| 57 | +In .NET 5 and later, the output is the following: |
| 58 | + |
| 59 | +Case sensitivity: |
| 60 | + When using CompareOptions.None, "ONE two" and "one TWO" are not equal. |
| 61 | + When using CompareOptions.IgnoreCase, "ONE two" and "one TWO" are equal. |
| 62 | +
|
| 63 | +Punctuation: |
| 64 | + When using CompareOptions.None, "hello world" and "hello, world!" are not equal. |
| 65 | + When using CompareOptions.IgnoreSymbols, "hello world" and "hello, world!" are equal. |
| 66 | +
|
| 67 | +Whitespace and mathematical symbols: |
| 68 | + When using CompareOptions.None, "3 + 5 = 8" and "358" are not equal. |
| 69 | + When using CompareOptions.IgnoreSymbols, "3 + 5 = 8" and "358" are equal. |
| 70 | +
|
| 71 | +Currency symbols, decimals and thousands separators: |
| 72 | + When using CompareOptions.None, "Total $15,000" and "Total: £150.00" are not equal. |
| 73 | + When using CompareOptions.IgnoreSymbols, "Total $15,000" and "Total: £150.00" are equal. |
| 74 | +
|
| 75 | +Half width and full width characters: |
| 76 | + When using CompareOptions.None, "abc,-" and "abc,-" are not equal. |
| 77 | + When using CompareOptions.IgnoreWidth, "abc,-" and "abc,-" are equal. |
| 78 | +
|
| 79 | +Hiragana and Katakana strings: |
| 80 | + When using CompareOptions.None, "ありがとう" and "アリガトウ" are not equal. |
| 81 | + When using CompareOptions.IgnoreKanaType, "ありがとう" and "アリガトウ" are equal. |
| 82 | +
|
| 83 | +Diacritical marks: |
| 84 | + When using CompareOptions.None, "café" and "cafe" are not equal. |
| 85 | + When using CompareOptions.IgnoreNonSpace, "café" and "cafe" are equal. |
| 86 | +
|
| 87 | +Ligature characters: |
| 88 | + When using CompareOptions.None, "straße œuvre cæsar" and "strasse oeuvre caesar" are not equal. |
| 89 | + When using CompareOptions.IgnoreNonSpace, "straße œuvre cæsar" and "strasse oeuvre caesar" are equal. |
| 90 | +
|
| 91 | +Note: When using .NET versions prior to .NET 5, ligature characters compare as equal to their |
| 92 | +non-ligature counterparts by default, so the last test will output as follows: |
| 93 | +
|
| 94 | +Ligature characters: |
| 95 | + When using CompareOptions.None, "straße œuvre cæsar" and "strasse oeuvre caesar" are equal. |
| 96 | + When using CompareOptions.IgnoreNonSpace, "straße œuvre cæsar" and "strasse oeuvre caesar" are equal. |
| 97 | +*/ |
0 commit comments