zoukankan      html  css  js  c++  java
  • 汉语转拼音pinyin4j

    分享一个将汉语转成拼音的工具包:pinyin4j-2.5.0.jar,下载地址:http://download.csdn.net/detail/abc_key/7629141

    使用例如以下代码

    import net.sourceforge.pinyin4j.PinyinHelper;
    
    public class HanyuToPinyin {
    
    	/*
    	 * 讲汉语转成拼音
    	 */
    	public static String ToHanyuPinyin(String sourceStr, boolean isUpperCase) {
    		int sourceLen = sourceStr == null ? 0 : sourceStr.length();
    		if (sourceLen < 1)
    			return "";
    		StringBuffer pinyinStrBuf = new StringBuffer();
    		for (int idx = 0; idx < sourceLen; idx++) {
    			String[] tmpData = PinyinHelper.toHanyuPinyinStringArray(sourceStr
    					.charAt(idx));
    			if (tmpData != null && tmpData.length > 0
    					&& tmpData[0].length() > 0) {
    				if (isUpperCase) {
    					pinyinStrBuf.append(tmpData[0].substring(0,
    							tmpData[0].length() - 1).toUpperCase());
    				} else {
    					pinyinStrBuf.append(tmpData[0].substring(0, tmpData[0]
    							.length() - 1));
    				}
    			}
    		}
    		return pinyinStrBuf.toString();
    	}
    	
    	/*
    	 * 汉语转成拼音的首字母
    	 */
    	public static String ToHanyuPinyinHead(String sourceStr, boolean isUpperCase) {
    		int sourceLen = sourceStr == null ? 0 : sourceStr.length();
    		if (sourceLen < 1)
    			return "";
    		StringBuffer pinyinStrBuf = new StringBuffer();
    		for (int idx = 0; idx < sourceLen; idx++) {
    			String[] tmpData = PinyinHelper.toHanyuPinyinStringArray(sourceStr
    					.charAt(idx));
    			if (tmpData != null && tmpData.length > 0
    					&& tmpData[0].length() > 0) {
    				if (isUpperCase) {
    					pinyinStrBuf.append(tmpData[0].substring(0,
    							1).toUpperCase());
    				} else {
    					pinyinStrBuf.append(tmpData[0].substring(0, 1));
    				}
    			}
    			//物品名称中不能转换成拼音的部分(英数)会原样保存
    			else{
    				pinyinStrBuf.append(sourceStr.charAt(idx));
    			}
    		}
    		return pinyinStrBuf.toString();
    	}
    }
    


  • 相关阅读:
    经济--1...19
    经济
    金融--
    经济--番外篇
    经济--基金问答
    经济--如何买基金?
    PHP面向对象常见的关键字和魔术方法
    php对象中类的继承性访问类型控制
    详解PHP的__set()、__get()、__isset()、unset()四个方法
    子类重载父类的方法“parent:方法名”
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5349175.html
Copyright © 2011-2022 走看看