zoukankan      html  css  js  c++  java
  • Java 拆分详细地址中的省、市、区、地址

    package com.taiping.test;
    
    import java.util.Arrays;
    
    public class Test14 {
        
        public static void main(String[] args) {
            
            String[] a = Test14.converDetailAddressToProvinceAndCityAndCountyAndStreet("广东省深圳市福田区新闻路2023号");
            
            System.out.println(a);
            
            for (int i = 0; i < a.length; i++) {
                System.out.println(a[i]);
            }
    //        广东省
    //        深圳市
    //        福田区
    //        新闻路2023号
        }
    
        /**
         * 处理详细地址拆分省 市 区 地址的转换关系
         * 
         * @param detailAddress
         * @return
         */
        public static String[] converDetailAddressToProvinceAndCityAndCountyAndStreet(
                String detailAddress) {
            String[] r = new String[4];
            try {
    
                String tempStr = detailAddress;
                String province = null;
                int provinceIdx = processProvince(tempStr);
                if (provinceIdx > -1) {
                    province = tempStr.substring(0, provinceIdx + 1);
                    tempStr = tempStr.substring(provinceIdx + 1);
                }
                ;
    
                String city = null;
                int cityIdx = processCity(tempStr);
                if (cityIdx > -1) {
                    city = tempStr.substring(0, cityIdx + 1);
                    tempStr = tempStr.substring(cityIdx + 1);
                }
    
                String county = null;
                int countyIdx = processCounty(tempStr);
                if (countyIdx > -1) {
                    county = tempStr.substring(0, countyIdx + 1);
                    tempStr = tempStr.substring(countyIdx + 1);
                }
                ;
    
                String street = tempStr;
    
                r[0] = province;
                r[1] = city;
                r[2] = county;
                r[3] = street;
            } catch (Exception e) {
                // 报错就直接返回r 为空即可。无法正常转义
            }
    
            return r;
        }
    
        // (?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)
        private static int processProvince(String s) {
            int[] idxs = new int[3];
            int provinceIdx = -1;
            if ((provinceIdx = s.indexOf("省")) > -1)
                idxs[0] = provinceIdx;
            provinceIdx = -1;
            if ((provinceIdx = s.indexOf("市")) > -1)
                idxs[1] = provinceIdx;
            provinceIdx = -1;
            if ((provinceIdx = s.indexOf("区")) > -1)
                idxs[2] = provinceIdx;
    
            Arrays.sort(idxs);
    
            for (int i = 0; i < idxs.length; i++) {
                int j = idxs[i];
                if (j > 0) {
                    return j;
                }
            }
    
            return provinceIdx;
        }
    
        // (?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)
        private static int processCity(String s) {
            int[] idxs = new int[7];
            int cityIdx = -1;
            if ((cityIdx = s.indexOf("县")) > -1)
                idxs[0] = cityIdx;
            cityIdx = -1;
            if ((cityIdx = s.indexOf("自治州")) > -1)
                idxs[1] = cityIdx + 2;
            cityIdx = -1;
            if ((cityIdx = s.indexOf("市辖区")) > -1)
                idxs[2] = cityIdx + 2;
            cityIdx = -1;
            if ((cityIdx = s.indexOf("市")) > -1)
                idxs[3] = cityIdx;
            cityIdx = -1;
            if ((cityIdx = s.indexOf("区")) > -1)
                idxs[4] = cityIdx;
            cityIdx = -1;
            if ((cityIdx = s.indexOf("地区")) > -1)
                idxs[5] = cityIdx + 1;
            cityIdx = -1;
            if ((cityIdx = s.indexOf("盟")) > -1)
                idxs[6] = cityIdx;
    
            Arrays.sort(idxs);
    
            for (int i = 0; i < idxs.length; i++) {
                int j = idxs[i];
                if (j > 0) {
                    return j;
                }
            }
    
            return cityIdx;
        }
    
        // (?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)
        private static int processCounty(String s) {
            int[] idxs = new int[6];
            int countyIdx = -1;
            if ((countyIdx = s.indexOf("县")) > -1)
                idxs[0] = countyIdx;
            countyIdx = -1;
            if ((countyIdx = s.indexOf("旗")) > -1)
                idxs[1] = countyIdx;
            countyIdx = -1;
            if ((countyIdx = s.indexOf("海域")) > -1)
                idxs[2] = countyIdx + 1;
            countyIdx = -1;
            if ((countyIdx = s.indexOf("市")) > -1)
                idxs[3] = countyIdx;
            countyIdx = -1;
            if ((countyIdx = s.indexOf("区")) > -1)
                idxs[4] = countyIdx;
            countyIdx = -1;
            if ((countyIdx = s.indexOf("岛")) > -1)
                idxs[5] = countyIdx;
    
            Arrays.sort(idxs);
    
            for (int i = 0; i < idxs.length; i++) {
                int j = idxs[i];
                if (j > 0) {
                    return j;
                }
            }
    
            return countyIdx;
        }
    }
    作者:小鱼
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    用批处理来启动/停止SQL SERVER 2005的服务 【转载】
    c#命名法 【转】
    oracle 隐式游标,显示游标,游标循环,动态SELECT语句和动态游标,异常处理,自定义异常【转载】
    fetch bulk collect into 批量效率的读取游标数据 【转载】
    Oracle 外连接和 (+)号的用法 【转载】
    如何在Oracle中复制表结构和表数据 【转载】
    Oracle 小知识点
    VSS 2005 配置(含录像) 【转载】
    json 详解 【转】
    .NET 2.0 使用最新版的JSON.net 进行反序列化 【转载】
  • 原文地址:https://www.cnblogs.com/sinosoft/p/14917889.html
Copyright © 2011-2022 走看看