introduce
Recently, when I made friends list, I imitated wechat friends list, A-Z index, and needed to use pinyin of friends’ names. Before, there was A tool class that encapsulated pinyin, but RECENTLY I found that for some polyphonic characters, the processing of surnames did not do bit, such as surname Single (Shan), because I did not do some processing for polyphonic characters, I just took the first one in the polyphonic word list, so the pinyin I got was Dan, so I used my spare time to deal with the tool class of this pinyin.
Common Surname Pinyin
SimpleArrayMap<Character, String> surnames = new SimpleArrayMap<>(35);
surnames.put('music'."yue");
surnames.put('乘'."sheng");
surnames.put('what'."nie");
surnames.put('revenge'."qiu");
surnames.put('can'."gui");
surnames.put('it'."pian");
surnames.put('area'."ou");
surnames.put('single'."shan");
surnames.put('and'."shen");
surnames.put('other'."gou");
surnames.put('call'."shao");
surnames.put('member'."yun");
surnames.put('Mi'."fu");
surnames.put(', '."fei");
surnames.put('discount'."she");
surnames.put('once'."zeng");
surnames.put('park'."piao");
surnames.put('check'."zha");
surnames.put('wash'."xian");
surnames.put('cover'."ge");
surnames.put('the'."zhai");
surnames.put('种'."chong");
surnames.put('the secret'."bi");
surnames.put('complex'."po");
surnames.put('Samuel,'."miao");
surnames.put('can'."nai");
surnames.put('sweet'."pi");
surnames.put('tain'."qin");
surnames.put(The 'solution'."xie");
surnames.put('change'."shan");
surnames.put('fitness'."kuo");
surnames.put('all'."du");
surnames.put('阿'."e");
surnames.put('hard'."ning");
surnames.put('black'."he");Copy the code
rendering
Acquisition of pinyin
The pinyin content captured above is as follows:
pinyin: QINTANG
pinyin: QIULAN
pinyin: SHANDAN
pinyin: ZENGER
pinyin: #Copy the code
You can see that the pinyin of the last name is correct, and the last one does not start with a Chinese character, so its pinyin content is #
The method of obtaining pinyin
Public static String getPinyin(String STR) {public static String getPinyin(String STR) {HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.UPPERCASE); / / set to uppercase format. SetToneType (HanyuPinyinToneType. WITHOUT_TONE); StringBuilder sb = new StringBuilder(); char[] charArray = str.toCharArray();for (int i = 0; i < charArray.length; i++) {
char c = charArray[i];
if(character.iswhitespace (c)) {// Skip if it is a spacecontinue;
}
if(isHanZi(c)) {// if the character is String s ="";
try {
ifS = getSurnamePinyin(string.valueof (c)); }else{/ / toHanyuPinyinStringArray returns a string array because it could be a Chinese character polyphone, here only to take the first result s = PinyinHelper. ToHanyuPinyinStringArray (c, the format) [0]; } sb.append(s); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); sb.append(s); }}else{// Not Chinese charactersif (i == 0) {
if(isEnglish(c)) {// If the first letter is a letter, return that letterreturn String.valueOf(c).toUpperCase(Locale.ENGLISH);
}
return "#"; // Return if not# no.}}}return sb.toString();
}Copy the code
The main logic is to break a string into an array of characters and iterate over each character:
- If the first character is a Chinese character, the method getSurnamePinyin() is called to get the corresponding pinyin, which is mainly the surname processing;
- If the first character is English, uppercase is returned.
- If the first character is neither Chinese nor English, the “#” sign is returned.
How to tell if it is Chinese characters
public static boolean isHanZi(char c) {
Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]+");
Matcher matcher = pattern.matcher(String.valueOf(c));
return matcher.matches();
}Copy the code
Using the above method, use the regular expression match, if yes, it is a Chinese character, return true.
How to tell if it is English
public static boolean isEnglish(char c) {
return String.valueOf(c).matches("^[a-zA-Z]*");
}Copy the code
Get the pinyin of the last name
public static String getSurnamePinyin(CharSequence name) {
if (name == null || name.length() == 0) return null;
char ch = name.charAt(0);
if (surnames.containsKey(ch)) {
String s = surnames.get(ch);
return s.toUpperCase(Locale.ENGLISH);
}
if (ch >= 0x4E00 && ch <= 0x9FA5) {
int sp = (ch - 0x4E00) * 6;
return pinyinTable.substring(sp, sp + 6).trim().toUpperCase(Locale.ENGLISH);
} else {
returnString.valueOf(ch).toUpperCase(Locale.ENGLISH); }}Copy the code
By looking for the pinyin of the corresponding surname already stored in the map
The relevant code
PinyinUtils code:
Github.com/chaychan/Bl…
Pinyin4j-2.5.0.jar 下载:
Raw.githubusercontent.com/chaychan/Bl…