Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 2.0
System
String Class
String Methods
Compare Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
String.Compare Method

Compares two specified String objects.

Name Description
String.Compare (String, String) Compares two specified String objects.

Supported by the .NET Compact Framework.

String.Compare (String, String, Boolean) Compares two specified String objects, ignoring or honoring their case.

Supported by the .NET Compact Framework.

String.Compare (String, String, StringComparison) Compares two specified String objects. A parameter specifies whether the comparison uses the current or invariant culture, honors or ignores case, and uses word or ordinal sort rules.

Supported by the .NET Compact Framework.

String.Compare (String, String, Boolean, CultureInfo) Compares two specified String objects, ignoring or honoring their case, and using culture-specific information to influence the comparison.

Supported by the .NET Compact Framework.

String.Compare (String, Int32, String, Int32, Int32) Compares substrings of two specified String objects.

Supported by the .NET Compact Framework.

String.Compare (String, Int32, String, Int32, Int32, Boolean) Compares substrings of two specified String objects, ignoring or honoring their case.

Supported by the .NET Compact Framework.

String.Compare (String, Int32, String, Int32, Int32, StringComparison) Compares substrings of two specified String objects.

Supported by the .NET Compact Framework.

String.Compare (String, Int32, String, Int32, Int32, Boolean, CultureInfo) Compares substrings of two specified String objects, ignoring or honoring their case, and using culture-specific information to influence the comparison.

Supported by the .NET Compact Framework.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Please specify what kind of comparison you expect :) Use StringComparison or CultureInfo!      Shawn Steele [MSFT]   |   Edit   |   Show History

Please specify what kind of comparison you expect :) Use StringComparison, or CultureInfo if necessary

Some applications need linguistic comparisons, others need ordinal type comparisons. A very general rule would be that user facing comparisons would need linguistic comparisons (use current culture), and security comparisons may need ordinal or ordinal ignore case. For some applilcations the default comparisons will not be appropriate. It is much better to understand the differences and use the correct version.

Additionally note that it is challenging to try to figure out what is going to be a match for the file system, URI/IRIs, etc. It is generally preferable to use the specific comparisons for the resource you're trying to compare. For example ?, & and other escaping rules in URLs, ..\, .\, :, etc in file names, etc. could all cause unexpected behavior if not properly tested for.

Tags What's this?: Add a tag
Flag as ContentBug
Comparison Results      Mike Hohm   |   Edit   |   Show History

I'm not sure why this is not already posted, but here is the logic table for the comparison results in general:

parameter 1 < parameter 2 = -1

parameter 1 > parameter 2 = 1

parameter 1 = parameter 2 = 0

The linked documents tell more. I'm not sure why I need to click on each overload, unless one returns different values. If that is the case, it probably needs a seperate method name.

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