w3resource

C#: Compare two substrings using different cultures and ignoring the case of the substrings

C# Sharp String: Exercise-23 with Solution

Write a C# Sharp program to compare two substrings using different cultures and ignore the substring case.

Sample Solution:-

C# Sharp Code:

using System;
using System.Globalization;

// Define the Example23 class
class Example23
{
    // Main method - entry point of the program
    public static void Main()
    {
        // Define and initialize string variables
        String str1 = "COMPUTER"; // String 1
        String str2 = "computer"; // String 2
        String str;               // String to store comparison result
        int result;               // Result of the comparison

        // Display initial strings
        Console.WriteLine();
        Console.WriteLine("\n str1 = '{0}', str2 = '{1}'", str1, str2);

        // Ignore case, Turkish culture comparison
        Console.WriteLine("Ignore case, Turkish culture:");
        // Compare a substring of str1 starting at index 4 with a substring of str2 starting at index 4, each with a length of 2, ignoring case using Turkish culture
        result = String.Compare(str1, 4, str2, 4, 2, true, new CultureInfo("tr-TR"));
        // Determine the relationship between the compared substrings
        str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
        // Display the comparison result
        Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1);
        Console.Write("{0} ", str);
        Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2);

        Console.WriteLine();
        // Ignore case, invariant culture comparison
        Console.WriteLine("Ignore case, invariant culture:");
        // Compare a substring of str1 starting at index 4 with a substring of str2 starting at index 4, each with a length of 2, ignoring case using invariant culture
        result = String.Compare(str1, 4, str2, 4, 2, true, CultureInfo.InvariantCulture);
        // Determine the relationship between the compared substrings
        str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
        // Display the comparison result
        Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1);
        Console.Write("{0} ", str);
        Console.WriteLine("substring '{0}' in '{1}'.\n\n", str2.Substring(4, 2), str2);
    }
}

Sample Output:

str1 = 'COMPUTER', str2 = 'computer'                                                                          
Ignore case, Turkish culture:                                                                                 
Substring 'UT' in 'COMPUTER' is equal to substring 'ut' in 'computer'.                                        
                                                                                                              
Ignore case, invariant culture:                                                                               
Substring 'UT' in 'COMPUTER' is equal to substring 'ut' in 'computer'. 

Flowchart :

Flowchart: C# Sharp Exercises - Compare two substrings using different cultures and ignoring the case of the substrings.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to compare two substrings that only differ in case. The first comparison ignores case and the second comparison considers case.
Next: Write a C# Sharp program to compare the last names of two people. It then lists them in alphabetical order.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.