a blog for those who code

Friday 18 July 2014

String Compare methods in C#

In this post we will tell you what are the different compare methods in C#.


string1.CompareTo(string2) 


It compares string1 with string2 and returns an integer that indicates whether string1 precedes, follows, or appears in the same position in the sort order as string2. In this comparison method null comes before a string value.

If integer value :

Less than zero : string1 precedes string2
Zero : string1 has the same position as string2
Greater than Zero : string1 follows string2, or string2 is null.

This string comparison method performs a word comparison using the current culture which means it will use culture dependent comparison.

string1.Equals(string2)


This method determines that string1 and string2 have the same value or not. This method performs case-senitive and culture-insensitive comparison. It will return true is the string2 is equal to string1 otherwise it will return false.
In this method null is not considered equal to anything.

string1 == string2

At first do understand that '==' is not equal to string.Equals(). String.Equals() will depend on the execution-time type of the target object whereas '==' will depend on the compile-time types of objects.

object string1 = new StringBuilder("string1").ToString();
object string2 = new StringBuilder("string1").ToString();

string1.Equals(string2) // true
string1 == string2 // false

Another difference is Equals will throw an exception when it call on null but '==' will give you the result.

string string1 = null;
string string2 = null;

string1.Equals(string2) // Exception
string1 == string2 // true

Object.ReferenceEquals(string1,string2)


It determines whether the specified string instances are the same instance. It returns true is string1 is the same instance as string2 or if both are null otherwise it will return false.

For Example :

string string1 = "String1";
string string2 = "String1";

Object.ReferenceEquals(string1,string2)


The above line will return true as string1 and string2 are equal because they are two instances os a single interned string.

string temp = "str";
string string1 = "String1" + temp;
string string2 = "String1" + temp;

Object.ReferenceEquals(string1,string2)


The above line will return false as string1 is not equal to string2 because the string are not interned.

String.CompareOrdinal(string1, string2)


It compares string1 and string2 by checking the numeric values of the corresponding Char objects in each string.

var int1 = String.CompareOrdinal(string1, string2)

int1 will be..

Less than zero - string1 is less than string2
Zero - string1 and string2 are equal
Greater than zero - string1 is greater than string2

It performs case-sensitive comparison. If both string1 and string2 are null the method returns 0 which indicates the strings are equal. If one of the value is null, the method considers the non-value as greater.

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment