省市县拆分工具

/***
 * 省市县拆分工具
 * @param address
 * @return
 */
public static String subString(String address){
    String i = "";
    int provinceIndex = address.indexOf("省");
    int cityIndex = address.indexOf("市");
    String province="";
    String city="";
    String county="";
    if(cityIndex>-1) {
        if(provinceIndex>-1) {
            province = address.substring(0, provinceIndex+1);
            city = address.substring(provinceIndex + 1, cityIndex+1);
        }else{
            province = address.substring(0, cityIndex+1);
             city = address.substring(0, cityIndex+1);
        }
        county = address.substring(cityIndex + 1);
        if(StringUtils.isEmpty(county)) {
            county = city;
        }
    }else {
        if(provinceIndex>-1) {
            province = address.substring(0, provinceIndex+1);
            city = address.substring(0, provinceIndex+1);
            county = address.substring(provinceIndex + 1);
            if(StringUtils.isEmpty(county)) {
                county = province;
            }
        }else{
            province = address;
            city = address;
            county = address;
        }
    }
    i=province+"/"+city+"/"+county;
    return i;
}

//省市县测试
public static void country(){
    String address0 = "河南省开封市鼓楼区";
    String address1 = "河南省";
    String address2 = "开封市";
    String address3 = "鼓楼区";
    String address4 = "开封市鼓楼区";
    String address5 = "河南省开封市";
    String address6 = "河南省鼓楼区";
    String address7 = "河南开封鼓楼";
    String strValidDate0 = subString(address0);
    String strValidDate1 = subString(address1);
    String strValidDate2 = subString(address2);
    String strValidDate3 = subString(address3);
    String strValidDate4 = subString(address4);
    String strValidDate5 = subString(address5);
    String strValidDate6 = subString(address6);
    String strValidDate7 = subString(address7);
    System.out.println(strValidDate0);
    System.out.println(strValidDate1);
    System.out.println(strValidDate2);
    System.out.println(strValidDate3);
    System.out.println(strValidDate4);
    System.out.println(strValidDate5);
    System.out.println(strValidDate6);
    System.out.println(strValidDate7);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值