汉字 转 汉语拼音

 

 通讯录排序

 

 

 

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

public class PinyinTransfer
{
	//汉字转拼音的方法
	public static String HanyuToPinyin(String name)
	{
		String pinyinName = "";
		char[] nameChar = name.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < nameChar.length; i++)
		{
			if (nameChar[i] > 128)
			{
				try
				{
					pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
			else {
				pinyinName += nameChar[i];
			}
		}
		return pinyinName;
	}

	public static void main(String[] args)
	{
		System.out.println(new PinyinTransfer().HanyuToPinyin("1"));
		System.out.println(new PinyinTransfer().HanyuToPinyin("包优"));
	}
	
	public static String chinessToPinyin(String chiness)
	{
		return HanyuToPinyin(chiness);
	}
}

 

 



 

 

 

 

 

你可能感兴趣的:(汉字,Pinyin,Transfer,转,汉语拼音)