.NET Framework Developer's Guide
Performing Culture-Insensitive String Operations

Most .NET Framework methods that perform culture-sensitive string operations by default provide method overloads that allow you to explicitly specify the culture to use by passing a CultureInfo parameter. These overloads allow you to eliminate cultural variations in case mappings and sorting rules and guarantee culture-insensitive results.

This section provides the following topics to demonstrate how to perform culture-insensitive string operations using .NET Framework methods that are culture-sensitive by default.

In This Section

Performing Culture-Insensitive String Comparisons

Describes how to use the String..::.Compare and String..::.CompareTo methods to perform culture-insensitive string comparisons.

Performing Culture-Insensitive Case Changes

Describes how to use the String..::.ToUpper, String..::.ToLower, Char..::.ToUpper, and Char..::.ToLower methods to perform culture-insensitive case changes.

Performing Culture-Insensitive String Operations in Collections

Describes how to use the CaseInsensitiveComparer Class, CaseInsensitiveHashCodeProvider class, SortedList Class, ArrayList.Sort Method and CollectionsUtil.CreateCaseInsensitiveHashtable Method to perform culture-insensitive operations in collections.

Performing Culture-Insensitive String Operations in Arrays

Describes how to use the Array..::.Sort and Array..::.BinarySearch methods to perform culture-insensitive operations in arrays.

Performing Culture-Insensitive Operations in the RegularExpressions Namespace

Describes how to perform culture-insensitive string operations using methods in the System.Text.RegularExpressions Namespace.

Related Sections

Culture-Insensitive String Operations

Describes why you should be aware of culture when performing operations on strings and provides guidelines for when to perform culture-sensitive operations and when to perform culture-insensitive operations.

Tags :


Community Content

David M. Kean - MSFT
Avoid using CultureInfo.InvariantCulture to perform comparisons

These guidelines are actually incorrect. While passing CultureInfo.InvariantCulture to the comparison methods does cause 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 one of the String.Compare overloads that takes a StringComparison value, and pass StringComparison.OrdinalIgnoreCase.

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 :

Page view tracker