简体   繁体   中英

How to get ISO 3166 Country Code from CultureInfo in .NET

Given a CultureInfo object, how do I get the two character ISO 3166 Country Code? Eg from en-US , I want US and from en-GB , I want GB . I also need to handle cases where the culture may not have a country code.

Use RegionInfo-class to retrieve two (or three) letter iso region name:

RegionInfo usa = new RegionInfo("en-US");           
string isoUSA = usa.TwoLetterISORegionName;

RegionInfo gb = new RegionInfo("en-GB");
string isoGB = gb.TwoLetterISORegionName;

You might want to catch possible exception that will happen if you try to pass invalid value to the constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM