StringComparer.Create Method

July 28, 2014

Creates a StringComparer object that compares strings according to the rules of a specified culture.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static StringComparer Create(
	CultureInfo culture,
	bool ignoreCase
)

Parameters

culture
Type: System.Globalization.CultureInfo
A culture whose linguistic rules are used to perform a string comparison.
ignoreCase
Type: System.Boolean
true to specify that comparison operations be case-insensitive; false to specify that comparison operations be case-sensitive.

Return Value

Type: System.StringComparer
A new StringComparer object that performs string comparisons according to the comparison rules used by the culture parameter and the case rule specified by the ignoreCase parameter.

ExceptionCondition
ArgumentNullException

culture is null.

The following code example demonstrates the properties and the Create method of the StringComparer class. The example illustrates how different StringComparer objects sort three versions of the Latin letter I.


// This example demonstrates members of the 
// System.StringComparer class.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create a list of string.
      List<string> list = new List<string>();

      // Get the tr-TR (Turkish-Turkey) culture.
      CultureInfo turkish = new CultureInfo("tr-TR");

      // Get the culture that is associated with the current thread.
      CultureInfo thisCulture = Thread.CurrentThread.CurrentCulture;

      // Get the standard StringComparers.
      StringComparer invCmp = StringComparer.InvariantCulture;
      StringComparer invICCmp = StringComparer.InvariantCultureIgnoreCase;
      StringComparer currCmp = StringComparer.CurrentCulture;
      StringComparer currICCmp = StringComparer.CurrentCultureIgnoreCase;
      StringComparer ordCmp = StringComparer.Ordinal;
      StringComparer ordICCmp = StringComparer.OrdinalIgnoreCase;

      // Create a StringComparer that uses the Turkish culture and ignores case.
      StringComparer turkICComp = StringComparer.Create(turkish, true);

      // Define three strings consisting of different versions of the letter I.
      // LATIN CAPITAL LETTER I (U+0049)
      string capitalLetterI = "I";

      // LATIN SMALL LETTER I (U+0069)
      string smallLetterI = "i";

      // LATIN SMALL LETTER DOTLESS I (U+0131)
      string smallLetterDotlessI = "\u0131";

      // Add the three strings to the list.
      list.Add(capitalLetterI);
      list.Add(smallLetterI);
      list.Add(smallLetterDotlessI);

      // Display the original list order.
      Display(outputBlock, list, "The original order of the list entries...");

      // Sort the list using the invariant culture.
      list.Sort(invCmp);
      Display(outputBlock, list, "Invariant culture...");
      list.Sort(invICCmp);
      Display(outputBlock, list, "Invariant culture, ignore case...");

      // Sort the list using the current culture.
      outputBlock.Text += String.Format("The current culture is \"{0}\".", thisCulture.Name) + "\n";
      list.Sort(currCmp);
      Display(outputBlock, list, "Current culture...");
      list.Sort(currICCmp);
      Display(outputBlock, list, "Current culture, ignore case...");

      // Sort the list using the ordinal value of the character code points.
      list.Sort(ordCmp);
      Display(outputBlock, list, "Ordinal...");
      list.Sort(ordICCmp);
      Display(outputBlock, list, "Ordinal, ignore case...");

      // Sort the list using the Turkish culture, which treats LATIN SMALL LETTER 
      // DOTLESS I differently than LATIN SMALL LETTER I.
      list.Sort(turkICComp);
      Display(outputBlock, list, "Turkish culture, ignore case...");
   }

   public static void Display(System.Windows.Controls.TextBlock outputBlock, List<string> lst, string title)
   {
      Char c;
      int codePoint;
      outputBlock.Text += title + "\n";
      foreach (string s in lst)
      {
         c = s[0];
         codePoint = Convert.ToInt32(c);
         outputBlock.Text += String.Format("0x{0:x}", codePoint) + "\n";
      }
      outputBlock.Text += "\n";
   }
}
/*
This code example produces the following results:

The original order of the list entries...
0x49
0x69
0x131

Invariant culture...
0x69
0x49
0x131

Invariant culture, ignore case...
0x49
0x69
0x131

The current culture is "en-US".
Current culture...
0x69
0x49
0x131

Current culture, ignore case...
0x49
0x69
0x131

Ordinal...
0x49
0x69
0x131

Ordinal, ignore case...
0x69
0x49
0x131

Turkish culture, ignore case...
0x131
0x49
0x69

*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft