Updated June 17, 2023
Introduction to C# Compare()
In C#, you can use the Compare() method to compare two strings. This integer value can be either less than zero, equal to zero, or greater than zero. The return value is less than zero if, among the two strings given, the first string precedes the second string in the order of sorting, and the return value equals zero. If both strings have the same value and the return value of the Compare() method is greater than zero; the second string comes after the first string in the sorting order.
Syntax:
The syntax is as follows:
String.Compare(string1, string2);
Where string1 is the first string that needs to be compared with the second string string2.
Working of C# Compare()
- Whenever there is a need to compare two strings in the sorting order and determine if the first string precedes the second string in the sorting order or if the second string follows the first string in the sorting order, or if the value of the two strings is equal, we make use of Compare() method in C#.
- The Compare() method returns zero if the two strings are equal in value.
- The Compare() method returns a value less than zero if, among the two strings given, the first string precedes the second string in the order of sorting.
Examples
Given below are the examples mentioned:
Example #1
C# program to demonstrate the use of the Compare() method to compare the two strings.
Code:
using System;
//a class called check is defined
public class check
{
//main method is called within which three string variables are defined to store three different strings
public static void Main(string[] args)
{
string string1 = "Welcome";
string string2 = "to";
string string3 = "C#";
//compare() method is used to compare two strings at a given time which returns an integer value less than zero if the first string precedes the second string in the sorting order or returns an integer value equal to zero if the first string is equal to the second string or returns an integer value greater than zero if the first string is followed by the second string in the sorting order
Console.WriteLine("The result of comparing the string1 and string2 is: {0}",string.Compare(string1,string2));
Console.WriteLine("The result of comparing the string2 and string3 is: {0}",string.Compare(string2,string3));
Console.WriteLine("The result of comparing the string3 and string1 is: {0}",string.Compare(string3,string1));
}
}
Output:
Explanation:
- The Compare() method compares two strings simultaneously. This method returns an integer value less than zero if the first string precedes the second string in the sorting order. It returns an integer value equal to zero if the first string is similar to the second string. Lastly, it returns an integer value greater than zero if the first string follows the second string in the sorting order.
Example #2
C# program to demonstrate the use of the Compare() method to compare the two strings.
Code:
using System;
//a class called check is defined
public class check
{
//main method is called within which three string variables are defined to store three different strings
public static void Main(string[] args)
{
string string1 = "Learning is fun";
string string2 = "Learning is fun";
string string3 = "fun";
//compare() method is used to compare two strings at a given time which returns an integer value less than zero if the first string precedes the second string in the sorting order or returns an integer value equal to zero if the first string is equal to the second string or returns an integer value greater than zero if the first string is followed by the second string in the sorting order
Console.WriteLine("The result of comparing the string1 and string2 is: {0}",string.Compare(string1,string2));
Console.WriteLine("The result of comparing the string2 and string3 is: {0}",string.Compare(string2,string3));
Console.WriteLine("The result of comparing the string3 and string1 is: {0}",string.Compare(string3,string1));
}
}
Output:
Explanation:
- In the above program, a class called check is defined. Then the main method is called, within which three string variables are defined to store three different strings.
Advantages
Given below are the advantages :
- The relative position of the strings in the order of sorting can be determined by making use of Compare() method in C#.
- If the given operands are strings and if we want to know if one string is preceding the other string or following the other string in the sorting order, Compare() method is one of the best options available in C#.
Conclusion
In this tutorial, we saw the concept of Compare() method in C# through definition, syntax, and working of Compare() method through programming examples and their outputs and the advantages of using Compare() method in our program while dealing with strings.
Recommended Articles
This is a guide to C# Compare(). Here we discuss the introduction along with working, advantages, and respective examples. You may also have a look at the following articles to learn more –