#import java.util.HashSet;
#import java.util.Set;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j. format。 。 # StringBuffer pybf = 新的 StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
/**
* 取得漢字符串拼音,英文字元不變
*
* @param chinese 漢字串
* @return 漢語拼音
*/
public static String cn2Spell(String chinese) {
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i if (arr [i] > 128) 嘗試 {
pybf. i], defaultFormat)[0]);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
pybf.append(arr[i]);
}
}
與「
}
public static String cnToSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.strbuf = new StringBuffer();
char[]toCharnameChar = chines.toCharraynamel. ();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType。 ; nameChar.length; i++) {
char name = quanbianban(nameChar[i]);
nameChar[i] = name;
if (128 嘗試{
strbuf.append(PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0));
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();# # }
} else {
strbuf.append(nameChar[i]);
}
}
pinyinName = strbuf.toString();
}
public static char quanbianban(char quan) {
switch (quan) {
case '0':
return '0';
case '1':
返回'1';
case '2':
返回'2';
# case '3':
return '3';
case '4':
# return '4';
# case '5':
return '5';
case '6':
返回'6';
case '7':
# 返回'7';
case '8':
返回'8' ;
case '9':
return '9';
預設值:
return quan;
}
}
/**
* 字串集合轉換字串(逗號分隔)
* @author wyh
* @param stringSet
* @return
*/
public static String makeStringByStringSet(Set
StringBuilder str = new StringBuilder(); =0;
for(String s : stringSet){
if(i == stringSet.size() - 1){
str.append(s);
}else{
str.append(s);
}else{
# str.append(s + ",");
}
i++;
}
return str.toString().toLowerCase();
} return str.toString().toLowerCase();
} return str.toString().toLowerCase();
*
* 取得拼音集合
* @author wyh
* @param src
* @return Set
*/
public static Set
if(src!=null && !src.trim().equalsIgnoreCase("")){ ## ] srcChar ;
srcChar=src.toCharArray();方式等
hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); ;
String[][] temp = new String[src.length()][];
for(int i=0;i
//是中文
//是中文或a-z或A-Z轉換拼音(我的需求,是保留中文或a-z或A-Z)
if(String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")){
# try{
temp[i] = PinyinHelper.toHanyuPinyinStringArray(srcChar[i], hanYuPinOutputFormat);
}catch(BadHanyuPinyinOutputFormatCombination e) {
}catch(BadHanyuPinyinOutputFormatCombination e) {
#> else if(((int)c>=65 && (int)c<=90) || ((int)c>=97 && (int)c<=122)){
temp[i] = new String[]{String.valueOf(srcChar[i])};
}else{
temp[i] = new String[]{""};
}
# }
StringString [] pingyinArray = Exchange(temp);
Set
# for(int i=0;i< add(pingyinArray[i]);
}
return pinyinSet;
}
return null; static String[] Exchange(String[][] strJaggedArray){
String[][] temp = DoExchange(strJaggedArray);
return temp[0];
}
##*# *
* 遞迴
* @author wyh
* @param strJaggedArray
* @return
*/
private static String[][] DoExchange(String[][] strJaggedArray){
int len = strJaggedArray.length;
if(len >= 2){ #int len1 = strJaggedArray[0].length;
int len2 = strJaggedArray[1].length;
int newlen = len1*len2;
String[] temp = new String[newlen]; #String[] temp = new String[newlen]; #String[] temp = new String[newlen]; #String[] temp = new String[newlen]; # #int Index = 0;
for(int i=0;i
Index ++;
}
}
String[][] newArray = new String[len-1][]; #String[][] newArray = new String[len-1][];
for(int i=2;i
}else{
return strJaggedArray;
}
}
public static void main(String[] args) throws Exception {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
# // UPPERCASE:大寫寫寫(ZHONG) ## zhong)
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
// WITHOUT_TONE:無音標 (zhong)
# // WITH_TONE_NUMBER:1-4數字表示英標/#hong# / WITH_TONE_MARK:直接用音標符號(必須WITH_U_UNICODE否則異常) (zhòng)
format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
WITH_Fv#n WITH_U_AND_COLON:用"u:"表示ü (nu:)
# // WITH_U_UNICODE:直接用ü (nü)
format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); StringArray( '重', format);
System.out.println(PinyinHelper.toHanyuPinyinStringArray('重', format)[1]);
System.out.println(PingYingChange.cnToSpell("鎮江abc")) ;
System.out.println(PingYingChange.quanbianban('o'));
String x = "嘅囧誰說壞學生來勼髮視頻襠児";
System.out.println( cn2FirstSpell(x));
System.out.println(cn2Spell(x));
String str = "單田芳";
)xSystem. ;
System.out.println("");
}
}
##
以上是pinyin4j的使用實例教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!