中文姓名姓氏拆分

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq136490497/article/details/79698014

中文姓名姓氏拆分

  • 最近遇到到一个拆分姓氏的需求
  • 有些特殊的姓氏需要按自己的逻辑处理 比如欧阳飞 的姓氏可能是欧阳也可能是 欧
  • 这里只是部分姓氏的匹配数据(据说姓氏总共有5000+)
  • 我实现的方式 如下
public class TestName1 {    
    private static String json="{\"RECORDS\":[{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"夏侯\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"欧阳\"},{\"surname\":\"爱新觉罗\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"羿\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"宿\"},{\"surname\":\"怀\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"寿\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"广\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"万俟\"},{\"surname\":\"司马\"},{\"surname\":\"上官\"},{\"surname\":\"诸葛\"},{\"surname\":\"闻人\"},{\"surname\":\"东方\"},{\"surname\":\"赫连\"},{\"surname\":\"皇甫\"},{\"surname\":\"尉迟\"},{\"surname\":\"公羊\"},{\"surname\":\"澹台\"},{\"surname\":\"公冶\"},{\"surname\":\"宗政\"},{\"surname\":\"濮阳\"},{\"surname\":\"淳于\"},{\"surname\":\"单于\"},{\"surname\":\"太叔\"},{\"surname\":\"申屠\"},{\"surname\":\"公孙\"},{\"surname\":\"仲孙\"},{\"surname\":\"轩辕\"},{\"surname\":\"令狐\"},{\"surname\":\"钟离\"},{\"surname\":\"宇文\"},{\"surname\":\"长孙\"},{\"surname\":\"慕容\"},{\"surname\":\"司徒\"},{\"surname\":\"司空\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"叶赫那拉\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"章佳\"},{\"surname\":\"那拉\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"依尔根觉罗\"},{\"surname\":\"依尔觉罗\"},{\"surname\":\"萨嘛喇\"},{\"surname\":\"赫舍里\"},{\"surname\":\"额尔德特\"},{\"surname\":\"萨克达\"},{\"surname\":\"钮祜禄\"},{\"surname\":\"他塔喇\"},{\"surname\":\"喜塔腊\"},{\"surname\":\"讷殷富察\"},{\"surname\":\"叶赫那兰\"},{\"surname\":\"库雅喇\"},{\"surname\":\"瓜尔佳\"},{\"surname\":\"舒穆禄\"},{\"surname\":\"索绰络\"},{\"surname\":\"纳喇\"},{\"surname\":\"乌雅\"},{\"surname\":\"范姜\"},{\"surname\":\"碧鲁\"},{\"surname\":\"张廖\"},{\"surname\":\"张简\"},{\"surname\":\"图门\"},{\"surname\":\"太史\"},{\"surname\":\"公叔\"},{\"surname\":\"乌孙\"},{\"surname\":\"完颜\"},{\"surname\":\"马佳\"},{\"surname\":\"佟佳\"},{\"surname\":\"富察\"},{\"surname\":\"费莫\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"鹿\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"第五\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"南宫\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"西门\"},{\"surname\":\"东门\"},{\"surname\":\"左丘\"},{\"surname\":\"梁丘\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"微生\"},{\"surname\":\"羊舌\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"呼延\"},{\"surname\":\"南门\"},{\"surname\":\"东郭\"},{\"surname\":\"百里\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"谷梁\"},{\"surname\":\"宰父\"},{\"surname\":\"夹谷\"},{\"surname\":\"拓跋\"},{\"surname\":\"壤驷\"},{\"surname\":\"乐正\"},{\"surname\":\"漆雕\"},{\"surname\":\"公西\"},{\"surname\":\"巫马\"},{\"surname\":\"端木\"},{\"surname\":\"颛孙\"},{\"surname\":\"子车\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"司寇\"},{\"surname\":\"亓官\"},{\"surname\":\"汤 \"},{\"surname\":\"庞 \"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"},{\"surname\":\"\"}]}";
    private NameInfo info;

    public static void main(String[] args) {
        new TestName1().getFirstName("李白", json);
    }

    private void getFirstName(String str,String json) {
        NameInfo info;
        ArrayList<name>names;
        String firstName=null;
        String lastName;

        Gson gson =new Gson();
        info=gson.fromJson(json, NameInfo.class);
        names=info.getRECORDS();

        for(int i=0;i<names.size();i++) {
            String surname=names.get(i).getSurname();
            if(str.contains(surname)) {
                //匹配中的姓氏 长度
                int length=surname.length();
                String mSurname=str.substring(0,length);
                if(mSurname.equals(surname)) {
                    firstName=surname;
                    System.out.println(str+" 的姓氏为: "+firstName);
//                  return;
                }
            }
        }

        if(firstName==null) {
            System.out.println(str+" 没有匹配到姓氏 ");
        }


    }

}

public static void main(String[] args) {
        new TestName1().getFirstName("李白", json);
    }
结果

>>>李白 的姓氏为: 李  

 public static void main(String[] args) {
        new TestName1().getFirstName("欧阳飞", json);
    }
结果
>>>欧阳飞 的姓氏为: 欧阳
欧阳飞 的姓氏为: 欧

有更好的方案告诉我哦

展开阅读全文

没有更多推荐了,返回首页