String.Equals Method (String, StringComparison)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Determines whether this string and a specified String object have the same value. A parameter specifies the culture, case, and sort rules used in the comparison.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.String
The string to compare to this instance.
- comparisonType
- Type: System.StringComparison
One of the enumeration values that specifies the rules for the comparison.
Return Value
Type: System.Booleantrue if the value of the value parameter is the same as this string; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentException | comparisonType is not a StringComparison value. |
The following code example uses three versions of the Equals method to determine whether a String object and a StringBuilder object are equal. The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed.
// This example demonstrates the // System.String.Equals(String, StringComparison) method. using System; using System.Threading; class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { string intro = "Compare three versions of the letter I using different " + "values of StringComparison."; // Define an array of strings where each element contains a version of the // letter I. (An array of strings is used so you can easily modify this // code example to test additional or different combinations of strings.) string[] threeIs = new string[3]; // LATIN SMALL LETTER I (U+0069) threeIs[0] = "\u0069"; // LATIN SMALL LETTER DOTLESS I (U+0131) threeIs[1] = "\u0131"; // LATIN CAPITAL LETTER I (U+0049) threeIs[2] = "\u0049"; string[] unicodeNames = { "LATIN SMALL LETTER I (U+0069)", "LATIN SMALL LETTER DOTLESS I (U+0131)", "LATIN CAPITAL LETTER I (U+0049)" }; StringComparison[] scValues = { StringComparison.CurrentCulture, StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCulture, StringComparison.InvariantCultureIgnoreCase, StringComparison.Ordinal, StringComparison.OrdinalIgnoreCase }; outputBlock.Text += intro + "\n"; // Display the current culture because the culture-specific comparisons // can produce different results with different cultures. outputBlock.Text += String.Format("The current culture is {0}.\n", Thread.CurrentThread.CurrentCulture.Name) + "\n"; // Determine whether three versions of the letter I are equal to each other. foreach (StringComparison sc in scValues) { outputBlock.Text += String.Format("StringComparison.{0}:", sc) + "\n"; // LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131) Test(outputBlock, 0, 1, sc, threeIs, unicodeNames); // LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049) Test(outputBlock, 0, 2, sc, threeIs, unicodeNames); // LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049) Test(outputBlock, 1, 2, sc, threeIs, unicodeNames); outputBlock.Text += "\n"; } } protected static void Test(System.Windows.Controls.TextBlock outputBlock, int x, int y, StringComparison comparison, string[] testI, string[] testNames) { string resultFmt = "{0} is {1}equal to {2}"; string result = "not "; // if (testI[x].Equals(testI[y], comparison)) result = ""; outputBlock.Text += String.Format(resultFmt, testNames[x], result, testNames[y]) + "\n"; } } /* This code example produces the following results: Compare three versions of the letter I using different values of StringComparison. The current culture is en-US. StringComparison.CurrentCulture: LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049) StringComparison.CurrentCultureIgnoreCase: LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049) StringComparison.InvariantCulture: LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049) StringComparison.InvariantCultureIgnoreCase: LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049) StringComparison.Ordinal: LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049) StringComparison.OrdinalIgnoreCase: LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131) LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049) */