views:

72

answers:

3

I used the code below to get the list of culture type, is their a way on how to get just the country name?

Thank you

        static void Main(string[] args)
       {

        StringBuilder sb = new StringBuilder();

        foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
        {
            sb.Append(ci.DisplayName);
            sb.AppendLine();
        }
        Console.WriteLine(sb.ToString());
        Console.ReadLine();


    }

Sample Output:

Spanish (Puerto Rico)

Spanish (United States)

A: 

Well, this regular expression seems to do the job in most cases:

        var regex = new System.Text.RegularExpressions.Regex(@"([\w+\s*\.*]+\))");
        foreach (var item in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
        {
            var match = regex.Match(item.DisplayName);
            string countryName = match.Value.Length == 0 ? "NA" : match.Value.Substring(0, match.Value.Length - 1);
            Console.WriteLine(countryName);
        }
Edgar Sánchez
DisplayName gives names like "German", "Catalan", "Finnish" etc. These are not exactly country names. Otherwise, we may use DisplayName or EnglishName.
Sidharth Panwar
In most cases DisplayName includes the country/region name surrounded by parenthesis, it's this last portion that we are getting with the regular expression. A bit of hack, but it works :-)
Edgar Sánchez
A: 

You can use the Name property of the CultureInfo to construct a RegionInfo. You can then use the DisplayName property. Try:

foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
            var ri = new RegionInfo(ci.Name);
            Console.WriteLine(ri.DisplayName);
}
Eric MSFT