Q:

Write a C# program to compare (less than, greater than, equal to ) two substrings

0

Write a C# program to compare (less than, greater than, equal to ) two substrings

All Answers

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

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..

using System;
 
class StringExample
{
    public static void Main()
    {
 
        String str1 = "computer";
        String str2 = "system";
        String str;
        int result;
 
        Console.WriteLine();
        Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
        result = String.Compare(str1, 2, str2, 0, 2);
        str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
        Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
        Console.Write("{0} ", str);
        Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
 
        Console.ReadLine();
    }
}

Result:

str1 = 'computer', str2 = 'system'

Substring 'mp' in 'computer' is less than substring 'sy' in 'system'.

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

total answers (1)

Write a C# program to find the number of times a s... >>
<< Write a C# program to sort a string array in ascen...