import java.util.HashMap;
import java.util.Map;
public class IDCardParser {
// 省市县名称映射表
private static final Map<String, String> AREA_MAP = new HashMap<>();
static {
// 初始化映射表
AREA_MAP.put("11", "北京市");
AREA_MAP.put("12", "天津市");
AREA_MAP.put("13", "河北省");
AREA_MAP.put("14", "山西省");
AREA_MAP.put("15", "内蒙古自治区");
AREA_MAP.put("21", "辽宁省");
AREA_MAP.put("22", "吉林省");
AREA_MAP.put("23", "黑龙江省");
AREA_MAP.put("31", "上海市");
AREA_MAP.put("32", "江苏省");
AREA_MAP.put("33", "浙江省");
AREA_MAP.put("34", "安徽省");
AREA_MAP.put("35", "福建省");
AREA_MAP.put("36", "江西省");
AREA_MAP.put("37", "山东省");
AREA_MAP.put("41", "河南省");
AREA_MAP.put("42", "湖北省");
AREA_MAP.put("43", "湖南省");
AREA_MAP.put("44", "广东省");
AREA_MAP.put("45", "广西壮族自治区");
AREA_MAP.put("46", "海南省");
AREA_MAP.put("50", "重庆市");
AREA_MAP.put("51", "四川省");
AREA_MAP.put("52", "贵州省");
AREA_MAP.put("53", "云南省");
AREA_MAP.put("54", "西藏自治区");
AREA_MAP.put("61", "陕西省");
AREA_MAP.put("62", "甘肃省");
AREA_MAP.put("63", "青海省");
AREA_MAP.put("64", "宁夏回族自治区");
AREA_MAP.put("65", "新疆维吾尔自治区");
AREA_MAP.put("71", "台湾省");
AREA_MAP.put("81", "香港特别行政区");
AREA_MAP.put("82", "澳门特别行政区");
}
public static String parseProvinceCityCounty(String idCardNumber) {
if (idCardNumber == null || idCardNumber.length() != 18) {
return "无效的身份证号码";
}
String provinceCode = idCardNumber.substring(0, 2);
String cityCode = idCardNumber.substring(2, 4);
String countyCode = idCardNumber.substring(4, 6);
String provinceName = AREA_MAP.get(provinceCode);
String cityName = getAreaName(cityCode);
String countyName = getAreaName(countyCode);
if (provinceName == null || cityName == null || countyName == null) {
return "无法识别的地区";
}
return provinceName + cityName + countyName;
}
private static String getAreaName(String code) {
String areaName = AREA_MAP.get(code);
return areaName != null ? areaName : "未知地区";
}
public static void main(String[] args) {
String idCardNumber = "310101198001010000"; // 示例身份证号码
String areaName = parseProvinceCityCounty(idCardNumber);
System.out.println("地区名称: " + areaName);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.