Export (0) Print
Expand All

String.Compare Method

Compares two specified String objects.

Overload List

Compares two specified String objects.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Compare(String, String) As Integer
[C#] public static int Compare(string, string);
[C++] public: static int Compare(String*, String*);
[JScript] public static function Compare(String, String) : int;

Compares two specified String objects, ignoring or honoring their case.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Compare(String, String, Boolean) As Integer
[C#] public static int Compare(string, string, bool);
[C++] public: static int Compare(String*, String*, bool);
[JScript] public static function Compare(String, String, Boolean) : int;

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.

[Visual Basic] Overloads Public Shared Function Compare(String, String, Boolean, CultureInfo) As Integer
[C#] public static int Compare(string, string, bool, CultureInfo);
[C++] public: static int Compare(String*, String*, bool, CultureInfo*);
[JScript] public static function Compare(String, String, Boolean, CultureInfo) : int;

Compares substrings of two specified String objects.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Compare(String, Integer, String, Integer, Integer) As Integer
[C#] public static int Compare(string, int, string, int, int);
[C++] public: static int Compare(String*, int, String*, int, int);
[JScript] public static function Compare(String, int, String, int, int) : int;

Compares substrings of two specified String objects, ignoring or honoring their case.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Compare(String, Integer, String, Integer, Integer, Boolean) As Integer
[C#] public static int Compare(string, int, string, int, int, bool);
[C++] public: static int Compare(String*, int, String*, int, int, bool);
[JScript] public static function Compare(String, int, String, int, int, Boolean) : int;

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.

[Visual Basic] Overloads Public Shared Function Compare(String, Integer, String, Integer, Integer, Boolean, CultureInfo) As Integer
[C#] public static int Compare(string, int, string, int, int, bool, CultureInfo);
[C++] public: static int Compare(String*, int, String*, int, int, bool, CultureInfo*);
[JScript] public static function Compare(String, int, String, int, int, Boolean, CultureInfo) : int;

Example

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Compare. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo)
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Class Sample
   
   Public Shared Sub Main()
      '                       0123456
      Dim str1 As [String] = "MACHINE"
      Dim str2 As [String] = "machine"
      Dim str As [String]
      Dim result As Integer
      
      Console.WriteLine()
      Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
      Console.WriteLine("Ignore case, Turkish culture:")
      result = [String].Compare(str1, 4, str2, 4, 2, True, New CultureInfo("tr-TR"))
      str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
      Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1)
      Console.Write("{0} ", str)
      Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2)
      
      Console.WriteLine()
      Console.WriteLine("Ignore case, invariant culture:")
      result = [String].Compare(str1, 4, str2, 4, 2, True, CultureInfo.InvariantCulture)
      str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
      Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1)
      Console.Write("{0} ", str)
      Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'str1 = 'MACHINE', str2 = 'machine'
'Ignore case, Turkish culture:
'Substring 'IN' in 'MACHINE' is less than substring 'in' in 'machine'.
'
'Ignore case, invariant culture:
'Substring 'IN' in 'MACHINE' is equal to substring 'in' in 'machine'.
'

[C#] 
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo)
using System;
using System.Globalization;

class Sample {
    public static void Main() {
//                 0123456
    String str1 = "MACHINE";
    String str2 = "machine";
    String str;
    int result;

    Console.WriteLine();
    Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
    Console.WriteLine("Ignore case, Turkish culture:");
    result = String.Compare(str1, 4, str2, 4, 2, true, new CultureInfo("tr-TR"));
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1);
    Console.Write("{0} ", str);
    Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2);

    Console.WriteLine();
    Console.WriteLine("Ignore case, invariant culture:");
    result = String.Compare(str1, 4, str2, 4, 2, true, CultureInfo.InvariantCulture);
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1);
    Console.Write("{0} ", str);
    Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2);
    }
}
/*
This example produces the following results:

str1 = 'MACHINE', str2 = 'machine'
Ignore case, Turkish culture:
Substring 'IN' in 'MACHINE' is less than substring 'in' in 'machine'.

Ignore case, invariant culture:
Substring 'IN' in 'MACHINE' is equal to substring 'in' in 'machine'.
*/

[C++] 
// Sample for String::Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo)
#using <mscorlib.dll>

using namespace System;
using namespace System::Globalization;

int main() {
   //                0123456
   String*  str1 = S"MACHINE";
   String*  str2 = S"machine";
   String* str;
   int result;

   Console::WriteLine();
   Console::WriteLine(S"str1 = '{0}', str2 = '{1}'", str1, str2);
   Console::WriteLine(S"Ignore case, Turkish culture:");
   result = String::Compare(str1, 4, str2, 4, 2, true, new CultureInfo(S"tr-TR"));
   str = ((result < 0) ? S"less than" : ((result > 0) ? S"greater than" : S"equal to"));
   Console::Write(S"Substring '{0}' in '{1}' is ", str1->Substring(4, 2), str1);
   Console::Write(S" {0} ", str);
   Console::WriteLine(S"substring '{0}' in '{1}'.", str2->Substring(4, 2), str2);

   Console::WriteLine();
   Console::WriteLine(S"Ignore case, invariant culture:");
   result = String::Compare(str1, 4, str2, 4, 2, true, CultureInfo::InvariantCulture);
   str = ((result < 0) ? S"less than" : ((result > 0) ? S"greater than" : S"equal to"));
   Console::Write(S"Substring '{0}' in '{1}' is ", str1->Substring(4, 2), str1);
   Console::Write(S" {0} ", str);
   Console::WriteLine(S"substring '{0}' in '{1}'.", str2->Substring(4, 2), str2);
}
/*
This example produces the following results:

str1 = 'MACHINE', str2 = 'machine'
Ignore case, Turkish culture:
Substring 'IN' in 'MACHINE' is less than substring 'in' in 'machine'.

Ignore case, invariant culture:
Substring 'IN' in 'MACHINE' is equal to substring 'in' in 'machine'.
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

String Class | String Members | System Namespace

Show:
© 2014 Microsoft