Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
 StringComparison Enumeration
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
StringComparison Enumeration

Updated: November 2007

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)

Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration StringComparison
Visual Basic (Usage)
Dim instance As StringComparison
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public enum StringComparison
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public enum class StringComparison
J#
/** @attribute SerializableAttribute */ 
/** @attribute ComVisibleAttribute(true) */
public enum StringComparison
JScript
public enum StringComparison
Member nameDescription
CurrentCulture Compare strings using culture-sensitive sort rules and the current culture.
CurrentCultureIgnoreCase Compare strings using culture-sensitive sort rules, the current culture, and ignoring the case of the strings being compared.
InvariantCulture Compare strings using culture-sensitive sort rules and the invariant culture.
InvariantCultureIgnoreCase Compare strings using culture-sensitive sort rules, the invariant culture, and ignoring the case of the strings being compared.
Ordinal Compare strings using ordinal sort rules.
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.

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. Using ordinal sort rules a string that starts with some Unicode character, U+xxxx, comes before a string that starts with some Unicode character, 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.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Prefer ordinal over invariant comparisons      David M. Kean - MSFT   |   Edit   |  

While passing StringComparison.InvariantCulture and StringComparison.InvariantCultureIgnoreCase to comparison methods causes them to perform culture-insensitive comparisons, it does not cause them to do non-linguistic comparisons. This is important when comparing identifiers such as file paths, registry keys, and environment variables, or making security decisions based on the result. Instead, use StringComparison.OrdinalIgnoreCase and StringComparison.Ordinal for such comparisons.

For more information on the correct way to compare strings, see the following article:

New Recommendations for Using Strings in Microsoft .NET 2.0
http://msdn2.microsoft.com/en-us/library/ms973919.aspx

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker