Q:

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

0

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'.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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);
   }
} 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now