在Java中验证了3个字符的ISO国家代码
仔细阅读,在回答之前请阅读评论强> 我不确定这是否真的很容易,我无法解决,或者我们需要做一些调整来检查这一点 我想验证请求中包含的3个字符的ISO国家代码是否有效,或者是否使用Java 2位国家/地区代码可以使用有效的 但我想验证3个字符的ISO代码在Java中验证了3个字符的ISO国家代码,java,Java,仔细阅读,在回答之前请阅读评论 我不确定这是否真的很容易,我无法解决,或者我们需要做一些调整来检查这一点 我想验证请求中包含的3个字符的ISO国家代码是否有效,或者是否使用Java 2位国家/地区代码可以使用有效的 但我想验证3个字符的ISO代码 Java版本:11基于3个字母代码获取国家名称的示例代码。调整,您可以创建自己的验证方法 public static void main(String[] args) { Map<String, String> countrie
Java版本:11基于3个字母代码获取国家名称的示例代码。调整,您可以创建自己的验证方法
public static void main(String[] args) {
Map<String, String> countriesMap = buildISOCodeAndCountryMap();
String countryName = countriesMap.get("IND");
System.out.println(countryName);
}
public static Map<String, String> buildISOCodeAndCountryMap() {
Map<String, String> isoCodeAndCountryMap= new HashMap<String, String>();
String[] isoCountries = Locale.getISOCountries();
for (String country : isoCountries) {
Locale locale = new Locale("", country);
String iso = locale.getISO3Country();
String code = locale.getCountry();
String name = locale.getDisplayCountry();
if (!"".equals(iso) && !"".equals(code)
&& !"".equals(name)) {
isoCodeAndCountryMap.put(iso, name);
}
}
return isoCodeAndCountryMap;
}
publicstaticvoidmain(字符串[]args){
Map countriesMap=buildISOCodeAndCountryMap();
字符串countryName=countriesMap.get(“IND”);
System.out.println(countryName);
}
公共静态地图buildISOCodeAndCountryMap(){
Map isoCodeAndCountryMap=新HashMap();
字符串[]isoCountries=Locale.getISOCountries();
for(字符串国家/地区:等国家/地区){
语言环境=新的语言环境(“,国家);
字符串iso=locale.getISO3Country();
字符串代码=locale.getCountry();
字符串名称=locale.getDisplayCountry();
如果(!“”.equals(iso)和(!“”.equals(代码)
&&!“”.equals(名称)){
isocode和countrymap.put(iso,名称);
}
}
返回等码和国家地图;
}
将此应用于两个字母的解决方案。这是否回答了您的问题?链接答案使用一组字符串。每个字符串中的字符数没有限制您使用的是什么版本的java?循环getAvailableLocales()
并调用getISO3Country