Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the country code from CultureInfo?

I have the following:

System.Globalization.CultureInfo c = new System.Globalization.CultureInfo("en-GB");  var a = c.DisplayName; var b = c.EnglishName; var d = c.LCID; var e = c.Name; var f = c.NativeName; var g = c.TextInfo; var h = c.ThreeLetterISOLanguageName; var i = c.ThreeLetterWindowsLanguageName; var j = c.TwoLetterISOLanguageName; 

None of this gives me the country code, e.g. GB.

Is there a way to get it without string splitting?

like image 742
Miguel Moura Avatar asked Dec 02 '13 14:12

Miguel Moura


People also ask

How can I get current country code in C#?

If you need the country name in the native language of that country, you can use CultureInfo. CurrentCulture. NativeName instead. If you need the two letter country code, you can use CultureInfo.CurrentCulture.Name, which returns "languageCode-countryCode" (e.g. en-CA).

What is CultureInfo?

The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.

What is CultureInfo CurrentCulture?

The CultureInfo. CurrentCulture property is a per-thread setting; that is, each thread can have its own culture. You get the culture of the current thread by retrieving the value of the CultureInfo. CurrentCulture property, as the following example illustrates. C# Copy.


2 Answers

var c = new CultureInfo("en-GB"); var r = new RegionInfo(c.LCID); string name = r.Name; 

Most probably you need to use r.TwoLetterISORegionName property.

string regionName = r.TwoLetterISORegionName; 
like image 179
Sriram Sakthivel Avatar answered Sep 19 '22 10:09

Sriram Sakthivel


System.Globalization.CultureInfo c = new System.Globalization.CultureInfo("en-GB"); var ri = new RegionInfo(c.Name); string countryName = ri.DisplayName; 

That will give you:

"United Kingdom" 

For Two Letter Use:

string countryAbbrivation = ri.TwoLetterISORegionName; 

That will give you "GB"

like image 33
Habib Avatar answered Sep 19 '22 10:09

Habib