Java中文姓名拆分
生活随笔
收集整理的這篇文章主要介紹了
Java中文姓名拆分
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java中文姓名拆分
實現邏輯比較簡單,就是通過姓氏匹配來完成。優先匹配復姓,不滿足是均按照單姓處理。這樣處理依舊會存在問題,復姓姓氏不全、少數民族特殊姓氏沒有匹配上的話都會按照單姓處理,所以還是需要不斷完善。 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() {} }
總結
以上是生活随笔為你收集整理的Java中文姓名拆分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 500 - 内部服务器错误. 您要查找的
- 下一篇: matlab plotroc 画roc曲