Posted by: Cirilo Meggiolaro | 04/9/2009

Tip of the day #177 – CompareOrdinal method

An easy and fast way to compare strings may be achieved by checking the code for the Char objects in each string. To perform that you must use the static CompareOrdinal method available under the String class.

Method signature

Two overloads are available for this method:

  • int CompareOrdinal(string strA, string strB);
  • int CompareOrdinal(string strA, int indexA, string strB, int indexB, int length);

How to…

The following code snippet shows two strings being compared using the CompareOrdinal method:

  • Console.WriteLine(string.CompareOrdinal(“ITTecture”, “ittecture”)); // Output: -32
  • Console.WriteLine(string.CompareOrdinal(“ittecture”, “ittecture”)); // Output: 0
  • Console.WriteLine(string.CompareOrdinal(“ittecture”, “ITTecture”)); // Output: 32

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: