How to Compare Strings in C#?
Last Updated :
24 Jan, 2022
A string is a collection of characters and is used to store plain text. Unlike C or C++, a string in C# is not terminated with a null character. The maximum size of a string object depends on the internal architecture of the system. A variable declared followed by “string” is actually an object of string class.
How to instantiate a string object?
We can create an object of string class by using variable name followed by “string” keyword.
Syntax:
string myString;
We can also initialize a string object at the time of declaration.
Syntax:
string myString = “GeeksForGeeks”;
This article focuses upon how we can compare strings in C#. For example, we are given three strings “GeeksforGeeks”, “Geeks” and “GeeksforGeeks”. Clearly first and last strings are the same. There are numerous ways to compare strings in C# out of which five ways are explained below in detail.
Method 1: Using String.Equals() method
The String class is specified in the .NET base class library. In other words, a String object is a sequential collection of System.Char objects which represent a string. The System.String class is immutable, that is once created its state we cannot make changes to it. String.Equals() method is a method of String class. This method takes two strings to be compared as parameters. It returns a logical value, true or false with the help of which we can determine whether the given strings are the same or not.
Syntax:
String.Equals(myString1, myString2)
Parameters: It takes two parameters myString1: First string and myString2: Second string
Return type: The return type of this method is Boolean. It will be true if both strings are equal and false if both strings are different
Example:
C#
using System;
class GFG{
static public void Main()
{
string myString1 = "GeeksforGeeks" ;
string myString2 = "Geeks" ;
string myString3 = "GeeksforGeeks" ;
if (String.Equals(myString1, myString2))
Console.WriteLine($ "{myString1} and {myString2} are same." );
else
Console.WriteLine($ "{myString1} and {myString2} are different." );
if (String.Equals(myString1, myString3))
Console.WriteLine($ "{myString1} and {myString3} are same." );
else
Console.WriteLine($ "{myString1} and {myString3} are different." );
if (String.Equals(myString2, myString3))
Console.WriteLine($ "{myString2} and {myString3} are same." );
else
Console.WriteLine($ "{myString2} and {myString3} are different." );
}
}
|
Output
GeeksforGeeks and Geeks are different.
GeeksforGeeks and GeeksforGeeks are same.
Geeks and GeeksforGeeks are different.
Method 2: Using String.Compare() method
This method is also defined under the String class. This method also takes two strings to be compared as parameters. It returns a numeric value depending upon the strings passed to the method. This method provides detailed information about the comparison of strings, that is why it is advantageous over String.equals() method.
Syntax:
String.Compare(myString1, myString2)
Parameters: It takes two parameters myString1: First string and myString2: Second string
Return type: The return type of this method is Integer. It will be:
- Less than zero: If the first string is lexicographically smaller than the second string.
- zero: If both strings are equal.
- Greater than zero: If the first string is lexicographically greater than the second string.
Example:
C#
using System;
class GFG{
static public void Main()
{
string myString1 = "GeeksforGeeks" ;
string myString2 = "Geeks" ;
string myString3 = "GeeksforGeeks" ;
if (String.Compare(myString1, myString2) == 0)
Console.WriteLine($ "{myString1} and {myString2} are same." );
else if (String.Compare(myString1, myString2) < 0)
Console.WriteLine(
$ "{myString1} is lexicographically smaller than {myString2}." );
else
Console.WriteLine(
$ "{myString1} is lexicographically greater than {myString2}." );
if (String.Compare(myString1, myString3) == 0)
Console.WriteLine($ "{myString1} and {myString3} are same." );
else if (String.Compare(myString1, myString3) < 0)
Console.WriteLine(
$ "{myString1} is lexicographically smaller than {myString3}." );
else
Console.WriteLine(
$ "{myString1} is lexicographically greater than {myString3}." );
if (String.Compare(myString2, myString3) == 0)
Console.WriteLine($ "{myString2} and {myString3} are same." );
else if (String.Compare(myString2, myString3) < 0)
Console.WriteLine(
$ "{myString2} is lexicographically smaller than {myString3}." );
else
Console.WriteLine(
$ "{myString2} is lexicographically greater than {myString3}." );
}
}
|
Output
GeeksforGeeks is lexicographically greater than Geeks.
GeeksforGeeks and GeeksforGeeks are same.
Geeks is lexicographically smaller than GeeksforGeeks.
Method 3: Using CompareTo() Method
This is an instance method defined under the String class and it is applied directly to a string or a string object. It returns a numeric value depending on the strings to be compared. This method provides detailed information about the comparison of strings, that is why it is advantageous over String.equals() method.
Syntax:
myString1.CompareTo(myString2)
Parameters: It takes one parameter that is myString2: Second string
Return type: The return type of this method is Integer. It will be:
- Less than zero: If the first string is lexicographically smaller than the second string.
- zero: If both strings are equal.
- Greater than zero: If the first string is lexicographically greater than the second string.
Example:
C#
using System;
class GFG{
static public void Main()
{
string myString1 = "GeeksforGeeks" ;
string myString2 = "Geeks" ;
string myString3 = "GeeksforGeeks" ;
if (myString1.CompareTo(myString2) == 0)
Console.WriteLine($ "{myString1} and {myString2} are same." );
else if (myString1.CompareTo(myString2) < 0)
Console.WriteLine(
$ "{myString1} is lexicographically smaller than {myString2}." );
else
Console.WriteLine(
$ "{myString1} is lexicographically greater than {myString2}." );
if (myString1.CompareTo(myString2) == 0)
Console.WriteLine($ "{myString1} and {myString3} are same." );
else if (myString1.CompareTo(myString2) < 0)
Console.WriteLine(
$ "{myString1} is lexicographically smaller than {myString3}." );
else
Console.WriteLine(
$ "{myString1} is lexicographically greater than {myString3}." );
if (myString1.CompareTo(myString2) == 0)
Console.WriteLine($ "{myString1} and {myString2} are same." );
else if (myString1.CompareTo(myString2) < 0)
Console.WriteLine(
$ "{myString2} is lexicographically smaller than {myString3}." );
else
Console.WriteLine(
$ "{myString2} is lexicographically greater than {myString3}." );
}
}
|
Output
GeeksforGeeks is lexicographically greater than Geeks.
GeeksforGeeks is lexicographically greater than GeeksforGeeks.
Geeks is lexicographically greater than GeeksforGeeks.
Method 4: Using compare() method of StringComparer class
This method is defined under the StringComparer class. We can create an object of this class with the help of which we can use Compare() method to compare two strings. This method provides detailed information about the comparison of strings, that is why it is advantageous over String.equals() method.
Syntax:
StringComparer myComparer = StringComparer.OrdinalIgnoreCase;
myComparer.Compare(myString1, myString2);
Parameters: It takes two parameters myString1: First string and myString2: Second string
Return type: The return type of this method is Integer. It will be:
- Less than zero: If the first string is lexicographically smaller than the second string.
- zero: If both strings are equal.
- Greater than zero: If the first string is lexicographically greater than the second string.
Example:
C#
using System;
class GFG{
static public void Main()
{
StringComparer myComparer = StringComparer.OrdinalIgnoreCase;
string myString1 = "GeeksforGeeks" ;
string myString2 = "Geeks" ;
string myString3 = "GeeksforGeeks" ;
if (myComparer.Compare(myString1, myString2) == 0)
Console.WriteLine($ "{myString1} and {myString2} are same." );
else if (myComparer.Compare(myString1, myString2) < 0)
Console.WriteLine(
$ "{myString1} is lexicographically smaller than {myString2}." );
else
Console.WriteLine(
$ "{myString1} is lexicographically greater than {myString2}." );
if (myComparer.Compare(myString1, myString3) == 0)
Console.WriteLine($ "{myString1} and {myString3} are same." );
else if (myComparer.Compare(myString1, myString3) < 0)
Console.WriteLine(
$ "{myString1} is lexicographically smaller than {myString3}." );
else
Console.WriteLine(
$ "{myString1} is lexicographically greater than {myString3}." );
if (myComparer.Compare(myString2, myString3) == 0)
Console.WriteLine($ "{myString2} and {myString3} are same." );
else if (myComparer.Compare(myString2, myString3) < 0)
Console.WriteLine(
$ "{myString2} is lexicographically smaller than {myString3}." );
else
Console.WriteLine(
$ "{myString2} is lexicographically greater than {myString3}." );
}
}
|
Output
GeeksforGeeks is lexicographically greater than Geeks.
GeeksforGeeks and GeeksforGeeks are same.
Geeks is lexicographically smaller than GeeksforGeeks.
Method 5: By comparing character by character (using custom compare method)
Strings can be compared character by character. Follow the steps below to compare two strings by using a custom compare method.
- Declare a static method Compare outside of the main method. Set the return type of this method as int.
- Initialize a variable len as the minimum of the lengths of both the strings.
- Iterate over index = 0 to index = len – 1 using a for loop. At each iteration compare corresponding characters of strings.
- If the first unmatched character of the first string at index is less than the character of the second string at index, then we will return -1.
- If the first unmatched character of the first string at index is less than the character of the second string at index, then we will return 1.
- After the end of the for-loop, if lengths of both strings are equal then return 0. If the length of the first string is less than the second string return -1 otherwise return 1.
- Call Compare() function from the main function by passing strings to be compared as parameters. If Compare() function returns 0 then print the first string is the same as the second string. If Compare() function returns -1 then the print first string is smaller than the second string else print first string is greater than the second string.
Example:
C#
using System;
class GFG{
static public int Compare( string myString1, string myString2)
{
int len = Math.Min(myString1.Length, myString2.Length);
for ( int index = 0; index < len; index++)
{
if (myString1[index] < myString2[index])
return -1;
else if (myString1[index] > myString2[index])
return 1;
}
if (myString1.Length == myString2.Length)
return 0;
return ((myString1.Length < myString2.Length) ? -1 : 1);
}
static public void Main()
{
string myString1 = "GeeksforGeeks" ;
string myString2 = "Geeks" ;
string myString3 = "GeeksforGeeks" ;
if (Compare(myString1, myString2) == 0)
Console.WriteLine($ "{myString1} and {myString2} are same." );
else if (Compare(myString1, myString3) < 0)
Console.WriteLine(
$ "{myString1} is lexicographically smaller than {myString2}." );
else
Console.WriteLine(
$ "{myString1} is lexicographically greater than {myString2}." );
if (Compare(myString1, myString3) == 0)
Console.WriteLine($ "{myString1} and {myString3} are same." );
else if (Compare(myString1, myString3) < 0)
Console.WriteLine(
$ "{myString1} is lexicographically smaller than {myString3}." );
else
Console.WriteLine(
$ "{myString1} is lexicographically greater than {myString3}." );
if (Compare(myString2, myString3) == 0)
Console.WriteLine($ "{myString2} and {myString3} are same." );
else if (Compare(myString2, myString3) < 0)
Console.WriteLine(
$ "{myString2} is lexicographically smaller than {myString3}." );
else
Console.WriteLine(
$ "{myString2} is lexicographically greater than {myString3}." );
}
}
|
Output
GeeksforGeeks is lexicographically greater than Geeks.
GeeksforGeeks and GeeksforGeeks are same.
Geeks is lexicographically smaller than GeeksforGeeks.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...