How to compare two strings in c#


To compare two strings in csharp there are various such method which compare them in different manner according to the developer’s condition such as having same string,  same string with different ordinals (uppercase or lowercase), starting and ending of strings value etc. the major types of comparison methods or function present in .Net framework 4.5 are:

  1. String.Compare
  2. String.compareOrdinal
  3. String.Compare to
  4. String.startwith
  5. String.endwith
  6. String.Equal

Lets have a look how above method works in different condition or programming situations.


It compares to strings and returns an integer value. Type of returned value can be a positive integer, a Zero or a negative integer. Positive value indicates the string is greater then previous. A zero indicates two strings are equal. And negative shows the first string is smaller then other. Code sample for this method:

 C# |  copy code |? 
string first = "this is first string";
        string secound = "this is first string";
        int value = String.Compare(first, secound);

Compare Ordinal

It works same as above compare method but without considering the culture of strings.

Compare To

It also work exactly same as compare method but the area of use is sorting.


This method compares two string and return bool value true or false.

Start With and Ends With

Both method checks whether two strings are start and end with same string value or not

Leave a Comment

Your email address will not be published.