import java.util.*;
/**
* @author : ZhiPengyu
* @ClassName : [NameUtil]
* @Description : [处理中文姓名相关]
* @CreateDate : [2021/10/20 16:31]
*/
public class NameUtils {
/**
* 复姓(两字),国内现存81个。末尾添加三字以上的部分满姓
*/
private static final String[] SURNAME_NOW = {"欧阳","太史","端木","上官","司马","东方","独孤","南宫","万俟","闻人","夏侯","诸葛","尉迟","公羊","赫连","澹台","皇甫","宗政","濮阳","公冶","太叔","申屠","公孙","慕容","仲孙","钟离","长孙","宇文","司徒","鲜于","司空","闾丘","子车","亓官","司寇","巫马","公西","颛孙","壤驷","公良","漆雕","乐正","宰父","谷梁","拓跋","夹谷","轩辕","令狐","段干","百里","呼延","东郭","南门","羊舌","微生","公户","公玉","公仪","梁丘","公仲","公上","公门","公山","公坚","左丘","公伯","西门","公祖","第五","公乘","贯丘","公皙","南荣","东里","东宫","仲长","子书","子桑","即墨","达奚","褚师","萨嘛喇","赫舍里","萨克达","钮祜禄","他塔喇","喜塔腊","库雅喇","瓜尔佳","舒穆禄","索绰络","叶赫那拉","依尔觉罗","额尔德特","讷殷富察","叶赫那兰","爱新觉罗","依尔根觉罗"};
/**
* 获取复姓,非单字姓氏。未匹配上则依旧返回单字姓氏
* @param name
* @return
*/
public static String getSurNameComplex(String name){
for(int i = 0;i < SURNAME_NOW.length;i++) {
if(name.startsWith(SURNAME_NOW[i])) {
return name.substring(0,SURNAME_NOW[i].length());
}
}
return name.substring(0,1);
}
/**
* 获取复姓名,去除姓氏后名
* @param name
* @return
*/
public static String getNameComplex(String name){
for(int i = 0;i < SURNAME_NOW.length;i++) {
if(name.startsWith(SURNAME_NOW[i])) {
return name.substring(SURNAME_NOW[i].length());
}
}
return name.substring(1);
}
/**
* 获取姓氏与姓名<br>
* 姓名在两字时,首字为姓。<br>
* 姓名大于两字时,优先匹配复姓。<br>
* 姓氏未匹配且姓名多于5字时,姓与名均在姓氏中,名为空;少于5字时则采用第一个字为姓。<br>
* @param name 姓名
* @return map类型数据,姓氏为key值“X”,名字为value值“M”
*/
public static Map<String,Object> getSurName(String name) {
Map<String,Object> mapData = new HashMap<>();
if (name.length() > 0 && name.length() <= 2){
mapData.put("X",name.substring(0,1));
mapData.put("M",name.substring(1));
} else if (name.length() > 2) {
// Arrays.asList(SURNAME).contains("")
for(int i=0;i<SURNAME_NOW.length;i++) {//遍历复姓数组
if (name.startsWith(SURNAME_NOW[i])){
mapData.put("X",SURNAME_NOW[i]);
mapData.put("M",name.substring(SURNAME_NOW[i].length()));
return mapData;
}
}
//姓氏没有匹配时,姓名多于5字符,姓与名均在姓氏中,否则采用第一个字为姓
mapData.put("X",name.length() >= 5 ? name : name.substring(0,1));
mapData.put("M",name.length() >= 5 ? "" : name.substring(1));
}
return mapData;
}
private NameUtils() {
}
}