import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class Test {
public static void main(String[] args) {
String input = "汉字转拼音";
// 创建汉语拼音处理类
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
// 输出设置,大小写,音标方式
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
// HanyuPinyinToneType.WITHOUT_TONE//无声调表示
// HanyuPinyinToneType.WITH_TONE_MARK//声调符号表示
// HanyuPinyinToneType.WITH_TONE_NUMBER//声调数字表示
char [] input_char = input.toCharArray();
String []pinyingStr = new String[input.length()];
StringBuffer out = new StringBuffer();
for(int i=0;i<input.length();i++){
try {
pinyingStr = PinyinHelper.toHanyuPinyinStringArray(input_char[i],defaultFormat);
out.append(pinyingStr[0]);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
System.out.println(out);
}
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class Test {
public static void main(String[] args) {
String input = "汉字转拼音";
// 创建汉语拼音处理类
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
// 输出设置,大小写,音标方式
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
// HanyuPinyinToneType.WITHOUT_TONE//无声调表示
// HanyuPinyinToneType.WITH_TONE_MARK//声调符号表示
// HanyuPinyinToneType.WITH_TONE_NUMBER//声调数字表示
char [] input_char = input.toCharArray();
String []pinyingStr = new String[input.length()];
StringBuffer out = new StringBuffer();
for(int i=0;i<input.length();i++){
try {
pinyingStr = PinyinHelper.toHanyuPinyinStringArray(input_char[i],defaultFormat);
out.append(pinyingStr[0]);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
}
System.out.println(out);
}
}
项目比较简单,主要是用来学习下,如果要开发的话需要看项目具体功能具体分析。。。
使用jar包:pinyin4j-2.5.0.jar,需要jar包的可以百度下