Friday, April 8, 2011

String comparison in C#

Comparing programmatic strings (these are never exposed to the end user)
String.Compare(string1, string2, StringComparison.OrdinalIgnoreCase)

Comparing strings that end user can see:
String.Compare(string1, string2, StringComparison.CurrentCultureIgnoreCase)

Wednesday, April 6, 2011

Reverse a string in C#

        static void way1(string testString)
        {
            Console.WriteLine("Way 1 ------------>");
            StringBuilder reverseString = new StringBuilder(testString.Length);
            for (int i = testString.Length - 1; i >= 0; i--)
            {
                reverseString.Append(testString[i]);
            }
            Console.WriteLine(reverseString.ToString());
        }

        static void way2(string testString)
        {
            Console.WriteLine("Way 2 ------------>");
            char []temp = testString.ToCharArray();
            Array.Reverse(temp);
            string reverseString = new string(temp);
            Console.WriteLine(reverseString);
        }

        static void way3(string testString)
        {
            Console.WriteLine("Way 3 ------------>");
            String reverseString = String.Empty;
            for (int i = testString.Length - 1; i >= 0; i--)
            {
                reverseString += testString[i];
            }
            Console.WriteLine(reverseString);
        }