How to compare two strings in c#

{0 Comments}

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.

Compare

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 |? 
1
string first = "this is first string";
2
        string secound = "this is first string";
3
        int value = String.Compare(first, secound);
4
        Response.Write(value);

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.

Equal

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.