码迷,mamicode.com
首页 > 其他好文 > 详细

从客户输入的地址中截取出省市级地址适用全中国,可应对各种杂乱地址

时间:2020-01-20 16:18:03      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:mon   字符   ext   esc   存在   lang   highlight   北京市   org   

package hello;

import org.apache.commons.lang3.StringUtils;

import java.util.*;
/**
 * @author bin.zhao
 * @description 获取一级+二级地址的字符串
 * @date 2020/1/20
 */
public class Demo {

    private static String AOMEN = "澳门特别行政区";
    private static String XIANGGANG = "香港特别行政区";
    private static String TAIWAN = "台湾";


    public static void main(String[] args) {
        String next = new Scanner(System.in).next();
        String[] s = spiltProvince(next);
        //打印获取到省级地址,市级地址,以及剩余地址
        Arrays.asList(s).forEach(System.out::println);
    }

    //截取出省级地址
    private static String[] spiltProvince(String address) {
        String[] totalAddress = {"", "", ""};
        if (StringUtils.isBlank(address)) {
            return null;
        }
        String sb = "";

        //对香港和澳门进行处理
        if (address.startsWith(AOMEN) || address.startsWith(XIANGGANG)) {
            sb = address.substring(7);
            totalAddress[0] = address.substring(0, 7);
            //截取出二级地址
            if (sb.startsWith("香港岛") || sb.startsWith("氹仔岛") || sb.startsWith("路环岛")) {
                //sb = address.substring(0, 10);
                totalAddress[1] = address.substring(7, 10);
                totalAddress[2] = address.substring(10);
            } else if (sb.startsWith("九龙") || sb.startsWith("新界")) {
                //sb = address.substring(0, 9);
                totalAddress[1] = address.substring(7, 9);
                totalAddress[2] = address.substring(9);
            } else if (sb.startsWith("澳门半岛")) {
                //sb = address.substring(0, 11);
                totalAddress[1] = address.substring(7, 11);
                totalAddress[2] = address.substring(11);
            }

            //对台湾进行处理
        } else if (address.startsWith(TAIWAN)) {
            totalAddress[0] = TAIWAN;
            //截取出二级地址
            boolean city = address.contains("市");
            boolean county = address.contains("县");
            //如果市级的索引小于县的索引,说明是市类型的二级地址,如果市和县都存在,比较市或者县的索引大小
            if ((city && !county) || (city && county && (address.indexOf("市") < address.indexOf("县")))) {
                //sb = address.substring(0, sb.indexOf("市") + 1);
                totalAddress[1] = address.substring(2, address.indexOf("市") + 1);
                totalAddress[2] = address.substring(address.indexOf("市") + 1);
            } else if ((!city && county) || (city && county && (address.indexOf("市") > address.indexOf("县")))) {
                //sb = address.substring(0, address.indexOf("县") + 1);
                totalAddress[1] = address.substring(2, address.indexOf("县") + 1);
                totalAddress[2] = address.substring(address.indexOf("县") + 1);
            }

            //自治区处理
        } else if (address.startsWith("内蒙古自治区") || address.startsWith("宁夏回族自治区") ||
                address.startsWith("西藏自治区") || address.startsWith("广西壮族自治区") ||
                address.startsWith("新疆维吾尔自治区")) {
            sb = getAddress(address, true, false);
            totalAddress[0] = sb.substring(0, sb.indexOf("区") + 1);
            totalAddress[1] = sb.substring(sb.indexOf("区") + 1);
            totalAddress[2] = address.substring(sb.length());
            //直辖市处理
        } else if (address.startsWith("北京市") || address.startsWith("上海市") ||
                address.startsWith("天津市") || address.startsWith("重庆市")) {
            sb = getAddress(address, false, true);
            totalAddress[0] = sb.substring(0, sb.indexOf("市") + 1);
            totalAddress[1] = sb.substring(sb.indexOf("市") + 1);
            totalAddress[2] = address.substring(sb.length());
            //省级类型的一级地址处理
        } else if (address.indexOf("省") != 0) {
            sb = getAddress(address, true, false);
            totalAddress[0] = sb.substring(0, sb.indexOf("省") + 1);
            totalAddress[1] = sb.substring(sb.indexOf("省") + 1);
            totalAddress[2] = address.substring(sb.length());
        }
        return totalAddress;
    }

    //根据指定地址和参数,对不同的一级地址来源进行一级\二级地址的截取
    private static String getAddress(String address, boolean state, boolean city) {
        String sb = "";
        Map<Integer, String> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        //自治区的判断
        if (state) {
            if (address.contains("市")) {
                list.add(address.indexOf("市"));
                map.put(address.indexOf("市"), "市");
            }
            if (address.contains("地区")) {
                list.add(address.indexOf("地区"));
                map.put(address.indexOf("地区"), "地区");
            }
            if (address.contains("盟")) {
                list.add(address.indexOf("盟"));
                map.put(address.indexOf("盟"), "盟");
            }
            if (address.contains("州")) {
                list.add(address.indexOf("州"));
                map.put(address.indexOf("州"), "州");
            }
            if (address.contains("级")) {
                list.add(address.indexOf("级"));
                map.put(address.indexOf("级"), "级");
            }
            if (address.contains("新区")) {
                list.add(address.indexOf("新区"));
                map.put(address.indexOf("新区"), "新区");
            }

            //直辖市的判断
        } else if (city) {
            if (address.contains("县")) {
                list.add(address.indexOf("县"));
                map.put(address.indexOf("县"), "县");
            }
            if (address.contains("区")) {
                list.add(address.indexOf("区"));
                map.put(address.indexOf("区"), "区");
            }
        }
        list.sort((o1, o2) -> {
            return o1 - o2;
        });
        String site = "";
        if (list.size() > 0) {
            site = map.get(list.get(0));
            if (site.length() == 2)
                sb = address.substring(0, address.indexOf(site) + 2);
            else
                sb = address.substring(0, address.indexOf(site) + 1);
        }
        return sb;
    }
}

  如果你的地址省级(包含自治区和特别行政区以及直辖市结尾的)和市级(包含市\盟\地区\自治州\直辖县级\新区等结尾)是选项框让客户选择,后面的是客户自行输入的地址,需要单独取出省市级和剩下的地址可以直接使用数组返回类型,如果需要获取省市地址的长度可以使用注掉的sb返回值;可以处理客户重复输入地址,以及客户输入的多个同级别的地址,判断获取选项框中的地址

从客户输入的地址中截取出省市级地址适用全中国,可应对各种杂乱地址

标签:mon   字符   ext   esc   存在   lang   highlight   北京市   org   

原文地址:https://www.cnblogs.com/bin-zhao/p/12218374.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!