Write a C# Sharp program to compare two strings in following three different ways produce three different results

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to compare two strings in following three different ways produce three different results.

a. using linguistic comparison for the en-US culture;
b. using linguistic case-sensitive comparison for the en-US culture;
c. using an ordinal comparison. It illustrates how the three methods of comparison

Expected Output :

'sister' comes before 'Sister'.                                                  
'sister' is the same as 'Sister'.                                                
'sister' comes after 'Sister'.

الأجوبة

using System;
using System.Globalization;
public class Example28
{
   public static void Main()
   {
      string str1 = "sister";
      string str2 = "Sister";
      string relation;
      int result;
      // Cultural (linguistic) comparison.
      result = String.Compare(str1, str2, new CultureInfo("en-US"), 
                              CompareOptions.None);
      if (result > 0)
         relation = "comes after";
      else if (result == 0)
         relation = "is the same as";
      else
         relation = "comes before";
      Console.WriteLine("'{0}' {1} '{2}'.", 
                        str1, relation, str2);
      // Cultural (linguistic) case-insensitive comparison.
      result = String.Compare(str1, str2, new CultureInfo("en-US"), 
                              CompareOptions.IgnoreCase);
      if (result > 0)
         relation = "comes after";
      else if (result == 0)
         relation = "is the same as";
      else
         relation = "comes before";
      Console.WriteLine("'{0}' {1} '{2}'.", 
                        str1, relation, str2);
       // Culture-insensitive ordinal comparison.
      result = String.CompareOrdinal(str1, str2);
      if (result > 0)
         relation = "comes after";
      else if (result == 0)
         relation = "is the same as";
      else
         relation = "comes before";

      Console.WriteLine("'{0}' {1} '{2}'.", 
                        str1, relation, str2);
   }
} 
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...