获取姓名的首字母工具类
This commit is contained in:
@@ -0,0 +1,204 @@
|
|||||||
|
package com.yxt.common.base.utils;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author dimengzhe
|
||||||
|
* @Date 2022/9/28 15:54
|
||||||
|
* @Description
|
||||||
|
*/
|
||||||
|
public class HanZiConverterPinYin {
|
||||||
|
|
||||||
|
public HanZiConverterPinYin() {
|
||||||
|
// 初始化汉字拼音字母对照表
|
||||||
|
initChinesePinyinComparisonMapList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 汉字拼音字母对照表
|
||||||
|
*/
|
||||||
|
private List<ChinesePinyinComparisonMap> chinesePinyinComparisonMapList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化汉字拼音字母对照表
|
||||||
|
*/
|
||||||
|
private void initChinesePinyinComparisonMapList() {
|
||||||
|
chinesePinyinComparisonMapList = new ArrayList<ChinesePinyinComparisonMap>();
|
||||||
|
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-20319, -20284, 'A'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-20283, -19776, 'B'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-19775, -19219, 'C'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-19218, -18711, 'D'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-18710, -18527, 'E'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-18526, -18240, 'F'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-18239, -17923, 'G'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-17922, -17418, 'H'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-17417, -16475, 'J'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-16474, -16213, 'K'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-16212, -15641, 'L'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-15640, -15166, 'M'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-15165, -14923, 'N'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-14922, -14915, 'O'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-14914, -14631, 'P'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-14630, -14150, 'Q'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-14149, -14091, 'R'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-14090, -13319, 'S'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-13318, -12839, 'T'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-12838, -12557, 'W'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-12556, -11848, 'X'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-11847, -11056, 'Y'));
|
||||||
|
chinesePinyinComparisonMapList.add(new ChinesePinyinComparisonMap(-11055, -10247, 'Z'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 遍历获取首字母
|
||||||
|
*/
|
||||||
|
public char getPY(char c) throws Exception {
|
||||||
|
byte[] bytes = String.valueOf(c).getBytes("GBK");
|
||||||
|
|
||||||
|
//双字节汉字处理
|
||||||
|
if (bytes.length == 2) {
|
||||||
|
|
||||||
|
int hightByte = 256 + bytes[0];
|
||||||
|
int lowByte = 256 + bytes[1];
|
||||||
|
int asc = (256 * hightByte + lowByte) - 256 * 256;
|
||||||
|
|
||||||
|
// 遍历转换
|
||||||
|
for (ChinesePinyinComparisonMap map : this.chinesePinyinComparisonMapList) {
|
||||||
|
if (asc >= map.getSAscll() && asc <= map.getEAscll()) {
|
||||||
|
return map.getCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 单字节或其他直接输入,不执行编码
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取汉字拼音
|
||||||
|
*/
|
||||||
|
public String getHanZiPY(String str) {
|
||||||
|
try {
|
||||||
|
StringBuilder pyStrBd = new StringBuilder();
|
||||||
|
|
||||||
|
for (char c : str.toCharArray()) {
|
||||||
|
pyStrBd.append(getPY(c));
|
||||||
|
}
|
||||||
|
|
||||||
|
return pyStrBd.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 汉字拼音字母对照类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
private class ChinesePinyinComparisonMap {
|
||||||
|
// 区间开头
|
||||||
|
private int sAscll;
|
||||||
|
|
||||||
|
// 区间结尾
|
||||||
|
private int eAscll;
|
||||||
|
|
||||||
|
// 对应字母
|
||||||
|
private char code;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Map<String, Object> DYZMAP = setDYZMap();
|
||||||
|
|
||||||
|
private static Map<String, Object> setDYZMap() {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("仇", "QIU");
|
||||||
|
map.put("柏", "BO");
|
||||||
|
map.put("牟", "MU");
|
||||||
|
map.put("颉", "XIE");
|
||||||
|
map.put("解", "XIE");
|
||||||
|
map.put("尉", "YU");
|
||||||
|
map.put("奇", "JI");
|
||||||
|
map.put("单", "SHAN");
|
||||||
|
map.put("谌", "SHEN");
|
||||||
|
map.put("乐", "YUE");
|
||||||
|
map.put("召", "SHAO");
|
||||||
|
map.put("朴", "PIAO");
|
||||||
|
map.put("区", "OU");
|
||||||
|
map.put("查", "ZHA");
|
||||||
|
map.put("曾", "ZENG");
|
||||||
|
map.put("缪", "MIAO");
|
||||||
|
map.put("晟", "CHENG");
|
||||||
|
map.put("员", "YUN");
|
||||||
|
map.put("贠", "YUN");
|
||||||
|
map.put("黑", "HE");
|
||||||
|
map.put("重", "CHONG");
|
||||||
|
map.put("秘", "BI");
|
||||||
|
map.put("冼", "XIAN");
|
||||||
|
map.put("折", "SHE");
|
||||||
|
map.put("翟", "ZHAI");
|
||||||
|
map.put("盖", "GE");
|
||||||
|
map.put("万俟", "MOQI");
|
||||||
|
map.put("尉迟", "YUCHI");
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPinYinFirst(String str) {
|
||||||
|
HanZiConverterPinYin hanZiConverterPinYin = new HanZiConverterPinYin();
|
||||||
|
char firstChar = str.toCharArray()[0];
|
||||||
|
String firstString = "";
|
||||||
|
if (Character.toString(firstChar).matches("^[\u4e00-\u9fa5]+$")) { // 为中文
|
||||||
|
char[] charPinYinChar = PinYinUtils.getCharPinYinChar(firstChar);
|
||||||
|
String result = "";
|
||||||
|
if (DYZMAP.containsKey(Character.toString(firstChar))) {
|
||||||
|
result = DYZMAP.get(Character.toString(firstChar)).toString().substring(0, 1);
|
||||||
|
} else {
|
||||||
|
result = StringUtils.join(charPinYinChar[0]);
|
||||||
|
}
|
||||||
|
firstString = result.toUpperCase();
|
||||||
|
} else if (Character.toString(firstChar).matches("^[a-zA-Z]")) { // 为英文字母
|
||||||
|
firstString = Character.toString(firstChar).toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
String code = hanZiConverterPinYin.getHanZiPY(str);
|
||||||
|
StringBuffer buffer = new StringBuffer(code);
|
||||||
|
code = buffer.replace(0, 1, firstString).toString();
|
||||||
|
System.out.println(code);
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
//测试
|
||||||
|
public static void main(String[] args) {
|
||||||
|
HanZiConverterPinYin hanZiConverterPinYin = new HanZiConverterPinYin();
|
||||||
|
String str = "仇闪";
|
||||||
|
char firstChar = str.toCharArray()[0];
|
||||||
|
String firstString = "";
|
||||||
|
if (Character.toString(firstChar).matches("^[\u4e00-\u9fa5]+$")) { // 为中文
|
||||||
|
char[] charPinYinChar = PinYinUtils.getCharPinYinChar(firstChar);
|
||||||
|
String result = "";
|
||||||
|
if (DYZMAP.containsKey(Character.toString(firstChar))) {
|
||||||
|
result = DYZMAP.get(Character.toString(firstChar)).toString().substring(0, 1);
|
||||||
|
} else {
|
||||||
|
result = StringUtils.join(charPinYinChar[0]);
|
||||||
|
}
|
||||||
|
firstString = result.toUpperCase();
|
||||||
|
} else if (Character.toString(firstChar).matches("^[a-zA-Z]")) { // 为英文字母
|
||||||
|
firstString = Character.toString(firstChar).toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
String code = hanZiConverterPinYin.getHanZiPY(str);
|
||||||
|
StringBuffer buffer = new StringBuffer(code);
|
||||||
|
code = buffer.replace(0, 1, firstString).toString();
|
||||||
|
System.out.println(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user