w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Demonstrate how culture can affect a comparison

C# Sharp String: Exercise-27 with Solution

Write a C# Sharp program to demonstrate how culture can affect a comparison.
Note : In Czech – Czech Republic culture, "ch" is a single character that is greater than "d". However, in English - United States culture, "ch" consists of two characters, and "c" is less than "d".

Sample Solution:-

C# Sharp Code:

using System;
using System.Globalization;

class Example27
{
    public static void Main() {
    String str1 = "change";
    String str2 = "dollar";
    String relation = null;

    relation = symbol( String.Compare(str1, str2, false, new CultureInfo("en-US")) );
    Console.WriteLine("\nFor en-US: {0} {1} {2}", str1, relation, str2);

    relation = symbol( String.Compare(str1, str2, false, new CultureInfo("cs-CZ")) );
    Console.WriteLine("For cs-CZ: {0} {1} {2}\n", str1, relation, str2);
    }

    private static String symbol(int r) {
    String s = "=";
    if      (r < 0) s = "<";
    else if (r > 0) s = ">";
    return s;
    }
}

Sample Output:

For en-US: change < dollar                                                                                    
For cs-CZ: change > dollar 

Flowchart :

Flowchart: C# Sharp Exercises - Demonstrate how culture can affect a comparison

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write C# Sharp program to demonstrate that the Compare(String, String, Boolean) method is equivalent to using ToUpper or ToLower when comparing strings.
Next: Write a C# Sharp program to compare two strings in following three different ways produce three different results.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming