String.Compare Method (String, String, Boolean)

Compares two specified String objects, ignoring or honoring their case, and returns an integer that indicates their relative position in the sort order.

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

public static int Compare(
	string strA,
	string strB,
	bool ignoreCase


Type: System.String
The first string to compare.
Type: System.String
The second string to compare.
Type: System.Boolean
true to ignore case during the comparison; otherwise, false.

Return Value

Type: System.Int32



Less than zero

strA is less than strB.


strA equals strB.

Greater than zero

strA is greater than strB.



Caution noteCaution

When comparing strings, you should call the Compare method, which requires that you explicitly specify the type of string comparison that the method uses. For more information, see Best Practices for Using Strings in the .NET Framework.




static bool IsFileURI(String path)


static bool IsFileURI(String path)

The following example demonstrates how this Compare method is equivalent to using ToUpper or ToLower when comparing strings.

    // Null terminated ASCII characters in an sbyte array
    String szAsciiUpper = null;
    sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 };
    // Instruct the Garbage Collector not to move the memory
    fixed(sbyte* pAsciiUpper = sbArr1)
        szAsciiUpper = new String(pAsciiUpper);
    String szAsciiLower = null;
    sbyte[] sbArr2 = { 0x61, 0x62, 0x63, 0x00 };
    // Instruct the Garbage Collector not to move the memory
    fixed(sbyte* pAsciiLower = sbArr2)
        szAsciiLower = new String(pAsciiLower, 0, sbArr2.Length);
    // Prints "ABC abc"
    Console.WriteLine(szAsciiUpper + " " + szAsciiLower);

    // Compare Strings - the result is true
    Console.WriteLine("The Strings are equal when capitalized ? " +
        (String.Compare(szAsciiUpper.ToUpper(), szAsciiLower.ToUpper())==0?"true":"false") );

    // This is the effective equivalent of another Compare method, which ignores case
    Console.WriteLine("The Strings are equal when capitalized ? " +
        (String.Compare(szAsciiUpper, szAsciiLower, true)==0?"true":"false") );

.NET Framework


.NET Framework Client Profile

Supported in: 4, 3.5 SP1


Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft