Partager via


StringComparer.CurrentCulture Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: October 2010

Gets a StringComparer object that performs a case-sensitive string comparison using the word comparison rules of the current culture.

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

Syntax

'Declaration
Public Shared ReadOnly Property CurrentCulture As StringComparer
public static StringComparer CurrentCulture { get; }

Property Value

Type: System.StringComparer
A new StringComparer object.

Remarks

The StringComparer returned by the CurrentCulture property can be used when strings are linguistically relevant. For example, if strings are displayed to the user, or if strings are the result of user interaction, culture-sensitive string comparison should be used to order the string data.

The current culture is the CultureInfo object associated with the current thread.

The CurrentCulture property actually returns an instance of an anonymous class derived from the StringComparer class.

Each call to the CurrentCulture property get accessor returns a new StringComparer object, as the following code shows.

Private Sub CompareCurrentCultureStringComparers(ByVal outputBlock As System.Windows.Controls.TextBlock)
   Dim stringComparer1 As StringComparer = StringComparer.CurrentCulture
   Dim stringComparer2 As StringComparer = StringComparer.CurrentCulture
   ' Displays False
   outputBlock.Text += StringComparer.ReferenceEquals(stringComparer1, _
                                                    stringComparer2) & vbCrLf
End Sub
private void CompareCurrentCultureStringComparer(System.Windows.Controls.TextBlock outputBlock)
{
   StringComparer stringComparer1 = StringComparer.CurrentCulture;
   StringComparer stringComparer2 = StringComparer.CurrentCulture;
   // Displays false
   outputBlock.Text += StringComparer.ReferenceEquals(stringComparer1,
                                                    stringComparer2) + "\n";
}

To improve performance, you can store the StringComparer object in a local variable rather than retrieve the value of the CurrentCulture property multiple times.

Examples

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 code example demonstrates members of the System.StringComparer class.

Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Threading

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Create a list of string.
      Dim list As New List(Of String)

      ' Get the tr-TR (Turkish-Turkey) culture.
      Dim turkish As New CultureInfo("tr-TR")

      ' Get the culture that is associated with the current thread.
      Dim thisCulture As CultureInfo = Thread.CurrentThread.CurrentCulture

      ' Get the standard StringComparers.
      Dim invCmp As StringComparer = StringComparer.InvariantCulture
      Dim invICCmp As StringComparer = StringComparer.InvariantCultureIgnoreCase
      Dim currCmp As StringComparer = StringComparer.CurrentCulture
      Dim currICCmp As StringComparer = StringComparer.CurrentCultureIgnoreCase
      Dim ordCmp As StringComparer = StringComparer.Ordinal
      Dim ordICCmp As StringComparer = StringComparer.OrdinalIgnoreCase

      ' Create a StringComparer that uses the Turkish culture and ignores case.
      Dim turkICComp As StringComparer = StringComparer.Create(turkish, True)

      ' Define three strings consisting of different versions of the letter I.
      ' LATIN CAPITAL LETTER I (U+0049)
      Dim capitalLetterI As String = "I"

      ' LATIN SMALL LETTER I (U+0069)
      Dim smallLetterI As String = "i"

      ' LATIN SMALL LETTER DOTLESS I (U+0131)
      Dim smallLetterDotlessI As String = "ı"

      ' 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) & vbCrLf
      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...")

   End Sub 'Main

   Public Shared Sub Display(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal lst As List(Of String), ByVal title As String)
      Dim c As Char
      Dim s As String
      Dim codePoint As Integer

      outputBlock.Text &= title & vbCrLf
      For Each s In lst
         c = s(0)
         codePoint = Convert.ToInt32(c)
         outputBlock.Text += String.Format("0x{0:x}", codePoint) & vbCrLf
      Next s
      outputBlock.Text &= vbCrLf
   End Sub 'Display
End Class 'Sample '

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

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

October 2010

Added exception information.

Content bug fix.