StringComparer.Create Method (CultureInfo, Boolean)


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

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

Public Shared Function Create (
	culture As CultureInfo,
	ignoreCase As Boolean
) As StringComparer


Type: System.Globalization.CultureInfo

A culture whose linguistic rules are used to perform a string comparison.

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.

Exception Condition

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

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

Class Sample

    Public Shared Sub Main() 
        ' 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"

        Dim smallLetterDotlessI As String = "ı"

        ' Add the three strings to the list.

        ' Display the original list order.
        Display(list, "The original order of the list entries...")

        ' Sort the list using the invariant culture.
        Display(list, "Invariant culture...")
        Display(list, "Invariant culture, ignore case...")

        ' Sort the list using the current culture.
        Console.WriteLine("The current culture is ""{0}"".", thisCulture.Name)
        Display(list, "Current culture...")
        Display(list, "Current culture, ignore case...")

        ' Sort the list using the ordinal value of the character code points.
        Display(list, "Ordinal...")
        Display(list, "Ordinal, ignore case...")

        ' Sort the list using the Turkish culture, which treats LATIN SMALL LETTER 
        ' DOTLESS I differently than LATIN SMALL LETTER I.
        Display(list, "Turkish culture, ignore case...")

    End Sub 'Main

    Public Shared Sub Display(ByVal lst As List(Of String), ByVal title As String)
        Dim c As Char
        Dim s As String
        Dim codePoint As Integer

        For Each s In lst
            c = s(0)
            codePoint = Convert.ToInt32(c)
            Console.WriteLine("0x{0:x}", codePoint)
        Next s
    End Sub 'Display
End Class 'Sample '

'This code example produces the following results:
'The original order of the list entries...
'Invariant culture...
'Invariant culture, ignore case...
'The current culture is "en-US".
'Current culture...
'Current culture, ignore case...
'Ordinal, ignore case...
'Turkish culture, ignore case...

.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top