酷酷七友软件定做 酷酷七友软件

软件开发

从京东收货地址提取省、市、区(c#实现)

以 从京东收货地址提取省、市、区(c#实现)

          某个项目里,只能获取到京东的收货人地址,为了能实现智能选择快递,特纯手工分析了一批地址样本,根据其规律,粗糙的写了个函数实现(未改良版)!详见代码:

 

 using (System.IO.StreamReader rd = new System.IO.StreamReader(Application.StartupPath + "\\1.txt",Encoding.Default))
            {
                while (rd.Peek() != -1)
                {

                    string str1 = rd.ReadLine();
                    if (str1 == "")
                        continue;
                    string str2 = str1;
                    if (str2.Length >= 8)
                        str2 = str2.Substring(0, 8);//怕后缀有省,市之类的名称干扰到截取
                    string province = "--", city = "--", county = "--";
                    if (!str2.Contains("省") && !str2.Contains("市"))//没省没市,应该是直辖市了
                    {
                        province=city = str1.Substring(0, 2);
                        county = str1.Split('区')[0].Replace(province, "").Trim();
                    }
                    else if (str2.Contains("省") && str2.Contains("市"))
                    {
                        var v1=str1.Split('省');
                        province =v1[0].Trim();// //str1.Split('省')[0].Trim();

                        var v2 = v1[1].Split('市');
                        city = v2[0].Trim();

                        if (str1.Contains("区"))
                            county = v2[1].Split('区')[0].Trim(); //这种方式即使出现二级市,也能获取到区
                        else if (str1.Contains("县"))
                            county = v2[1].Split('县')[0].Trim();
                        else //区,县都没的话就取市后两们吧
                            county = v2[1].Substring(0, 2).Trim();
 
                    }
                    else if (str2.Contains("市") && !str2.Contains("省"))//只有市
                    {
                        //黑龙江哈尔滨市道外区
                        var v2 = str1.Split('市');
                        if (v2[0].Length == 6)//省和市都三个字
                        {
                            province = v2[0].Substring(0, 3); city = v2[0].Substring(3, 3);
                        }
                        else if (v2[0].Length == 5)//这种就特别,先排除黑龙江,内蒙古
                        {
                            if (str2.Contains("内蒙古") || str2.Contains("黑龙江"))
                            { province = v2[0].Substring(0, 3); city = v2[0].Substring(3, 2); }
                            else
                            {
                                province = v2[0].Substring(0, 2); city = v2[0].Substring(2, 3);
                            }
                           
                        }
                        else //剩下的按两个字一组分
                        {
                          province = v2[0].Substring(0, 2); city = v2[0].Substring(2, 2);
                        }

                        //河北保定市北市区城区秀兰  二级市要处理?
                        if (v2[1].Contains("县"))//   == "")
                            county = v2[1].Split('县')[0].Trim();
                        else if(v2[1].Contains("区"))
                         { county = v2[1].Split('区')[0].Trim(); }
                         else if(v2[1].Contains("镇"))
{ county = v2[1].Split('镇')[0].Trim(); }

                           
                            
                    }

                    if (city.Length == 1) city = city + "市";//
                    System.Diagnostics.Debug.Print("{0}>>{1}>>{2} ={3}", province, city,county,str1.Substring(0,12));
               
                }
           
            }

注意:这里并没对二级市做过多的判断(项目只需精确到省和市),实际应用中,应根据需要做调整。对于区和县的获取,实际很难做到100%精确。


  byError Q:302777528