Thursday 23 August 2012

String.Compare Methods in C#

Different types of String Compare Methods Examples 

public class StringCompareExample
      {
            public static void Main()
            {
                  string s1 = "Dot\u00ADNet"; // '\u00AD' Unicode Character 'SOFT HYPHEN'
                  string s2 = "DotNet";                    
                  Console.WriteLine("Comparison of '{0}' and '{1}': {2}", s1, s2, String.Compare(s1, s2, true));           
                 
                  Console.WriteLine("2nd Example : ");
                  String MyString = "Hello World1";
                  String OtherString = "Hello World";
                  int MyInt = MyString.CompareTo(OtherString);
                  Console.WriteLine( MyInt );
                 
                  Console.WriteLine("3rd Example : ");
                  string MyString1 = "Hello World";
                  string YourString = "Hello World1";
                  Console.WriteLine(String.Equals(MyString1, YourString));

                  Console.WriteLine("4th Example : ");
                  string MyString2 = "Hello World";
                  Console.WriteLine(MyString2.StartsWith("Hello"));
                 
                  // String.Compare Methods
                  Console.WriteLine("5th Example : ");
                  string str1 = null;
                  string str2 = null;
                 
                  str1 = "csharp";
                  str2 = "CSharp";
                 
                  int result = 0;
                 
                  result = string.Compare(str1, str2);
                  Console.WriteLine(result.ToString());                
                 
                  result = string.Compare(str1, str2, true);                 
                  Console.WriteLine(result.ToString());
                 
                  result = string.Compare(str1, str2, false);                
                  Console.WriteLine(result.ToString());
                 
                  Console.ReadLine();
            }
      }

No comments:

Post a Comment