Export (0) Print
Expand All

CompareInfo::Compare Method (String, Int32, String, Int32)

Compares the end section of a string with the end section of another string.

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

public:
virtual int Compare(
	String^ string1, 
	int offset1, 
	String^ string2, 
	int offset2
)

Parameters

string1
Type: System::String

The first string to compare.

offset1
Type: System::Int32

The zero-based index of the character in string1 at which to start comparing.

string2
Type: System::String

The second string to compare.

offset2
Type: System::Int32

The zero-based index of the character in string2 at which to start comparing.

Return Value

Type: System::Int32

Value

Condition

zero

The two strings are equal.

less than zero

The specified section of string1 is less than the specified section of string2.

greater than zero

The specified section of string1 is greater than the specified section of string2.

ExceptionCondition
ArgumentOutOfRangeException

offset1 or offset2 is less than zero.

-or-

offset1 is greater than or equal to the number of characters in string1.

-or-

offset2 is greater than or equal to the number of characters in string2.

If a security decision depends on a string comparison or a case change, the application should use the InvariantCulture to ensure that the behavior is consistent regardless of the culture settings of the operating system.

The following code example compares portions of two strings using the different CompareInfo objects:

using namespace System;
using namespace System::Globalization;
int main()
{

   // Defines the strings to compare.
   String^ myStr1 = "calle";
   String^ myStr2 = "calor";

   // Uses GetCompareInfo to create the CompareInfo that  
   // uses the S"es-ES" culture with international sort.
   CompareInfo^ myCompIntl = CompareInfo::GetCompareInfo( "es-ES" );

   // Uses GetCompareInfo to create the CompareInfo that  
   // uses the S"es-ES" culture with traditional sort.
   CompareInfo^ myCompTrad = CompareInfo::GetCompareInfo( 0x040A );

   // Uses the CompareInfo property of the InvariantCulture.
   CompareInfo^ myCompInva = CultureInfo::InvariantCulture->CompareInfo;

   // Compares two strings using myCompIntl.
   Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1->Substring( 2 ), myStr2->Substring( 2 ) );
   Console::WriteLine( "   With myCompIntl::Compare: {0}", myCompIntl->Compare( myStr1, 2, myStr2, 2 ) );
   Console::WriteLine( "   With myCompTrad::Compare: {0}", myCompTrad->Compare( myStr1, 2, myStr2, 2 ) );
   Console::WriteLine( "   With myCompInva::Compare: {0}", myCompInva->Compare( myStr1, 2, myStr2, 2 ) );
}

/*
This code produces the following output.

Comparing "lle" and "lor"
   With myCompIntl::Compare: -1
   With myCompTrad::Compare: 1
   With myCompInva::Compare: -1
*/

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, 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, Zune

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, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft