[英]How to get country code from locale?
I want to get country codes from a list of locale. 我想从语言环境列表中获取国家/地区代码。 eg I have inputs: en_US, dua_CM, zh-Hans_MO ... I want to output corresponding country code: US, CM, MO... (I am not sure if country code is the last two characters of locale...)
例如,我有输入:en_US,dua_CM,zh-Hans_MO ...我想输出相应的国家代码:US,CM,MO ...(我不确定国家代码是否是语言环境的最后两个字符...)
I tried the following Java code, but it printed out null. 我尝试了以下Java代码,但将其打印为空。
Locale lc = new Locale("en_US");
System.out.print(lc.getCountry());
Any suggestions? 有什么建议么? Thanks in advance!
提前致谢!
Dan 担
Here, you are defining your own Locale
, for which you haven't specified a Country value (see the overloaded Locale
constructors). 在这里,您要定义自己的
Locale
,但尚未为其指定Country值(请参阅重载的Locale
构造函数)。
Instead, use 相反,使用
Locale lc = Locale.forLanguageTag("en-US"); // Java 1.7
which will get the Locale
object from Java's supported locales . 它将从Java支持的locales中获取
Locale
对象。
Also, related 另外,相关
(read to the end) (读到最后)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.