拼音工具類PinyinUtils

2021-09-11 13:24:43 字數 2326 閱讀 3605

******arraymapsurnames = new ******arraymap<>(35);

surnames.put('樂', "yue");

surnames.put('乘', "sheng");

surnames.put('乜', "nie");

surnames.put('仇', "qiu");

surnames.put('會', "gui");

surnames.put('便', "pian");

surnames.put('區', "ou");

surnames.put('單', "shan");

surnames.put('參', "shen");

surnames.put('句', "gou");

surnames.put('召', "shao");

surnames.put('員', "yun");

surnames.put('宓', "fu");

surnames.put('弗', "fei");

surnames.put('折', "she");

surnames.put('曾', "zeng");

surnames.put('樸', "piao");

surnames.put('查', "zha");

surnames.put('洗', "xian");

surnames.put('蓋', "ge");

surnames.put('祭', "zhai");

surnames.put('種', "chong");

surnames.put('秘', "bi");

surnames.put('繁', "po");

surnames.put('繆', "miao");

surnames.put('能', "nai");

surnames.put('蕃', "pi");

surnames.put('覃', "qin");

surnames.put('解', "xie");

surnames.put('諶', "shan");

surnames.put('適', "kuo");

surnames.put('都', "du");

surnames.put('阿', "e");

surnames.put('難', "ning");

surnames.put('黑', "he");複製**

image

上圖獲取的拼音內容如下:

pinyin: qintang

pinyin: qiulan

pinyin: shandan

pinyin: zenger

pinyin: #

複製**

可以看到姓氏的拼音是正確的,最後乙個由於不是以漢字開頭,所以其拼音的內容是#

public static string getpinyin(string str) 

if (ishanzi(c)) else

} catch (badhanyupinyinoutputformatcombination e)

} else

return

"#"; // 不是的話返回#號}}

}return sb.tostring();

}複製**

主要的邏輯是,將字串拆解成字元陣列,遍歷每個字元:

如果第乙個是漢字,則獲取呼叫getsurnamepinyin()方法,獲取其對應的拼音,這個方法主要是對姓氏的處理;

如果第乙個字元是英文,則直接返回該字元的大寫;

如果第乙個字元不是漢字也不是英文,則直接返回"#"號;

public static boolean ishanzi(char c) 複製**
通過如上方法,使用正規表示式匹配,如果滿足,則說明是漢字,返回true。

public static boolean isenglish(char c) 複製**
public static string getsurnamepinyin(charsequence name) 

if (ch >= 0x4e00 && ch <= 0x9fa5) else

}複製**

通過查詢已經儲存在map中對應姓氏的拼音

拼音工具類

import net.sourceforge.pinyin4j.pinyinhelper import net.sourceforge.pinyin4j.format.hanyupinyincasetype import net.sourceforge.pinyin4j.format.hanyupi...

漢字轉拼音工具類

public class pinyin4jutils 將字串轉換成拼音陣列 param src return public static string stringtopinyin string src,string separator 將字串轉換成拼音陣列 param src param ispo...

漢字轉拼音工具類

package com.example.demo.utils import net.sourceforge.pinyin4j.pinyinhelper author suyanlong date created in 2020 6 30 14 18 description ceshi version...