Windows Phone Developers

Thursday, September 15, 2011

String Comparison (case sensitive and case-insensitive) in C#/VB.NET

How to IgnoreCase in C#/VB.NET String Comparison

Whether you like or not, life revolves around comparison . Here let us see how to compare two strings using Equals method.

Equals method takes two arguments - the string to be compared and the comparison type (Case Sensitive or Insensitive)

The following snippet gives a hint of both:


        private static void StringCompExample()
        {
            string s1 = "MulTIpleCASE";
            string s2 = "MultipleCase";

            bool compare_result = false;

            //case sensitive comparison
            compare_result = s1.Equals(s2);
            Console.WriteLine(compare_result.ToString ());
           
            //case insensitive comparison
            compare_result = s1.Equals(s2, StringComparison.InvariantCultureIgnoreCase);
            Console.WriteLine(compare_result.ToString());
           
        }
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment