Q:

Write a C# Sharp program to compare the current string instance with another string

0

Write a C# Sharp program to compare the current string instance with another string. 

Expected Output :

The strings occur in the same position in the sort order.                        
The first string follows the second in the sort order.                           
                                                                                 
The first string precedes the second in the sort order.                          
The first string precedes the second in the sort order.                          
The first string follows the second in the sort order.

All Answers

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

using System;
public class Example33
{
   public static void Main()
   {
      string str1 = "Goodbye";
      string str2 = "Hello";
      string str3 = "a small string";
      string str4 = "goodbye";
      // Compare a string to itself.
      Console.WriteLine(CompareStrings(str1, str1));
      Console.WriteLine(CompareStrings(str1, str2));
      Console.WriteLine(CompareStrings(str1, str3));
      // Compare a string to another string that varies only by case.
      Console.WriteLine(CompareStrings(str1, str4));
      Console.WriteLine(CompareStrings(str4, str1));
   }
   private static string CompareStrings( string str1, string str2 )
   {
      // Compare the values, using the CompareTo method on the first string.
      int cmpVal = str1.CompareTo(str2);

	   if (cmpVal == 0) // The strings are the same.
         return "The strings occur in the same position in the sort order.";
      else if (cmpVal > 0)
         return "The first string precedes the second in the sort order.";
      else
         return "The first string follows the second in the sort order.";
    }
}

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