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.
Given below are the examples mentioned:
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:
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:
Given below are the advantages :
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.
The above is the detailed content of C# Compare(). For more information, please follow other related articles on the PHP Chinese website!