w3resource

C#: Compare three versions of the letter "I"

C# Sharp String: Exercise-29 with Solution

Write a C# Sharp program to compare three versions of the letter "I". The results are affected by culture choice, whether the case is ignored, and whether an ordinal comparison is performed.

Sample Solution:-

C# Sharp Code:

using System;
using System.Threading;

class Example29 
{
    // The Main method serves as the entry point of the program.
    public static void Main() 
    {
        // The intro string provides a description of the example.
        string intro = "Compare three versions of the letter I using different " + 
                       "values of StringComparison.";

        // Define an array of strings, each representing a version of the letter I.
        string[] threeIs = new string[3];
        threeIs[0] = "\u0069";  // LATIN SMALL LETTER I (U+0069)
        threeIs[1] = "\u0131";  // LATIN SMALL LETTER DOTLESS I (U+0131)
        threeIs[2] = "\u0049";  // LATIN CAPITAL LETTER I (U+0049)

        // An array containing names for each Unicode character used for comparison.
        string[] unicodeNames = 
        {
            "LATIN SMALL LETTER I (U+0069)", 
            "LATIN SMALL LETTER DOTLESS I (U+0131)", 
            "LATIN CAPITAL LETTER I (U+0049)"
        };

        // An array containing different StringComparison values for comparison.
        StringComparison[] scValues = {
            StringComparison.CurrentCulture,
            StringComparison.CurrentCultureIgnoreCase,
            StringComparison.InvariantCulture,
            StringComparison.InvariantCultureIgnoreCase,
            StringComparison.Ordinal,
            StringComparison.OrdinalIgnoreCase
        };

        Console.Clear();
        Console.WriteLine(intro);

        // Display the current culture to highlight the impact of culture-specific comparisons.
        Console.WriteLine("The current culture is {0}.\n", 
                           Thread.CurrentThread.CurrentCulture.Name);

        // Determine the relative sort order of three versions of the letter I using different StringComparison values.
        foreach (StringComparison sc in scValues)
        {
            Console.WriteLine("StringComparison.{0}:", sc);

            // Perform comparisons among the versions of the letter I based on the StringComparison value.
            Test(0, 1, sc, threeIs, unicodeNames);
            Test(0, 2, sc, threeIs, unicodeNames);
            Test(1, 2, sc, threeIs, unicodeNames);

            Console.WriteLine();
        }
    }

    // Method to perform and display the comparison results between characters at indices x and y.
    protected static void Test(int x, int y, StringComparison comparison, string[] testI, string[] testNames)
    {
        string resultFmt = "{0} is {1} {2}";
        string result = "equal to";
        int cmpValue = 0;

        // Perform a string comparison between the characters at indices x and y using the specified StringComparison value.
        cmpValue = String.Compare(testI[x], testI[y], comparison);

        // Determine the relationship between the characters based on the comparison result.
        if (cmpValue < 0) 
            result = "less than";
        else if (cmpValue > 0)
            result = "greater than";

        // Display the comparison result between characters at indices x and y.
        Console.WriteLine(resultFmt, testNames[x], result, testNames[y]);
    }
}

Sample Output:

Compare three versions of the letter I using different values of StringComparison.                            
The current culture is en-US.                                                                                 
                                                                                                              
StringComparison.CurrentCulture:                                                                              
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)                              
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)                                    
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)                         
                                                                                                              
StringComparison.CurrentCultureIgnoreCase:                                                                    
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)                              
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)                                     
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)                         
                                                                                                              
StringComparison.InvariantCulture:                                                                            
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)                              
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)                                    
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)                         
                                                                                                              
StringComparison.InvariantCultureIgnoreCase:                                                                  
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)                              
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)                                     
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)                         
                                                                                                              
StringComparison.Ordinal:                                                                                     
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)                              
LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049)                                 
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)                         
                                                                                                              
StringComparison.OrdinalIgnoreCase:                                                                           
LATIN SMALL LETTER I (U+0069) is equal to LATIN SMALL LETTER DOTLESS I (U+0131)                               
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)                                     
LATIN SMALL LETTER DOTLESS I (U+0131) is equal to LATIN CAPITAL LETTER I (U+0049) 

Flowchart :

Flowchart: C# Sharp Exercises - Compares three versions of the letter 'I'.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to compare two strings in following three different ways produce three different results.
Next: Write a C# Sharp program to demonstrate that CompareOrdinal and Compare use different sort orders.

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.