0 out of 3 rated this helpful - Rate this topic

StringComparison Enumeration

Updated: January 2012

Specifies the culture, case, and sort rules to be used by certain overloads of the String.Compare and String.Equals methods.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
public enum StringComparison
Member name Description
Supported by the XNA Framework Supported by Portable Class Library CurrentCulture Compare strings using culture-sensitive sort rules and the current culture.
Supported by the XNA Framework Supported by Portable Class Library CurrentCultureIgnoreCase Compare strings using culture-sensitive sort rules, the current culture, and ignoring the case of the strings being compared.
Supported by the XNA Framework InvariantCulture Compare strings using culture-sensitive sort rules and the invariant culture.
Supported by the XNA Framework InvariantCultureIgnoreCase Compare strings using culture-sensitive sort rules, the invariant culture, and ignoring the case of the strings being compared.
Supported by the XNA Framework Supported by Portable Class Library Ordinal Compare strings using ordinal sort rules.
Supported by the XNA Framework Supported by Portable Class Library OrdinalIgnoreCase Compare strings using ordinal sort rules and ignoring the case of the strings being compared.

The StringComparison enumeration is used to specify whether a string comparison should use the current culture or the invariant culture, word or ordinal sort rules, and be case-sensitive or case-insensitive.

Important note Important

When you call a string comparison method such as String.Compare, String.Equals, or String.IndexOf, you should always call an overload that includes a parameter of type StringComparison so that you can specify the type of comparison that the method performs. For more information, see Best Practices for Using Strings in the .NET Framework.

An operation that uses word sort rules performs a culture-sensitive comparison wherein certain nonalphanumeric Unicode characters might have special weights assigned to them. Using word sort rules and the conventions of a specific culture, the hyphen ("-") might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list.

An operation that uses ordinal sort rules performs a comparison based on the numeric value (Unicode code point) of each Char in the string. An ordinal comparison is fast but culture-insensitive. When you use ordinal sort rules to sort strings that start with Unicode characters (U+), the string U+xxxx comes before the string U+yyyy if the value of xxxx is numerically less than yyyy.

For more information about comparisons, see the System.String class remarks. For more information about culture, see the System.Globalization.CultureInfo class remarks. For guidelines on when to use ordinal or culture-sensitive comparison rules or the rules of the invariant culture, see Best Practices for Using Strings in the .NET Framework.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Date

History

Reason

January 2012

Added an alert to the Remarks section.

Information enhancement.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Undocumented Feature ...
It is a shame that the documentation does not mention the very special implementation of IndexOf when using StringComparison.Ordinal.

Performance increases by some magnitude, as mentioned in these references:
http://stackoverflow.com/questions/4904705/boyer-moore-practical-in-c
http://www.blackbeltcoder.com/Articles/algorithms/fast-text-search-with-boyer-moore

Invariant enumerations definitions

Shouldn't Invariant enumerations have "culture-insensitive" instead of "culture-sensitive" in their definitions?

Culture Sensitivity and the Invariant Culture

Because the invariant culture is based on the cultural conventions of the English language (but without regard to English as it is spoken in a particular region), it is culture-sensitive rather than culture-insensitive. It differs from other cultures in two major ways: its cultural data is stable across versions of the .NET Framework; and its data is invariant and cannot be overridden by user settings. So while the invariant culture can be seen as less culture-sensitive than other cultures, it nevertheless is culture-sensitive.

I hope that this helps to clarify the confusion about the description of StringComparison.InvariantCulture and StringComparison.InvariantCultureIgnoreCase as culture-sensitive.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation