Java Code Examples for net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat#setCaseType()

The following examples show how to use net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat#setCaseType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChineseToEnglish.java    From AndroidDemo with MIT License 6 votes vote down vote up
/**
 * 返回一个字的拼音
 */
public static String toPinYin(char hanzi) {
    HanyuPinyinOutputFormat hanyuPinyin = new HanyuPinyinOutputFormat();
    hanyuPinyin.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    hanyuPinyin.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    hanyuPinyin.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
    String[] pinyinArray = null;
    try {
        //是否在汉字范围内
        if (hanzi >= 0x4e00 && hanzi <= 0x9fa5) {
            pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi, hanyuPinyin);
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
        Log.e(TAG, "toPinYin: hanzi = "+hanzi );
        Log.e(TAG, "toPinYin: pinyinArray.toString() = "+pinyinArray.toString() );
    }
    //将获取到的拼音返回
    if (pinyinArray != null && pinyinArray.length > 0) {
        return pinyinArray[0];
    } else {
        Log.e(TAG, "toPinYin: hanzi = "+hanzi );
        return "#";
    }
}
 
Example 2
Source File: StringHelper.java    From openzaly with Apache License 2.0 6 votes vote down vote up
public static String toLatinPinYin(String text) {
	// format for string
	HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
	format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写
	format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

	char[] input = StringUtils.trimToEmpty(text).toCharArray();
	StringBuffer result = new StringBuffer();
	try {
		for (char ch : input) {
			if (Character.toString(ch).matches("[\\u4E00-\\u9FA5]+")) {
				String[] temp = PinyinHelper.toHanyuPinyinStringArray(ch, format);
				result.append(temp[0]);
			} else
				result.append(Character.toString(ch));
		}
	} catch (BadHanyuPinyinOutputFormatCombination e) {
		logger.error("string to latin pinyin error", e);
	}
	return result.toString();
}
 
Example 3
Source File: ChinesUtil.java    From utils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取汉字串拼音首字母,英文字符不变.
 *
 * @param chinese 汉字串
 * @return 汉语拼音首字母
 */
public static String getFirstSpell(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 < arr.length; i++) {
        if (arr[i] > 128) {
            try {
                String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                if (temp != null) {
                    pybf.append(temp[0].charAt(0));
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            pybf.append(arr[i]);
        }
    }
    return pybf.toString().replaceAll("\\W", "").trim();
}
 
Example 4
Source File: HanyuPinyinHelper.java    From opscenter with Apache License 2.0 6 votes vote down vote up
public static String getPinyinString(String ChineseLanguage) {
    char[] cl_chars = ChineseLanguage.trim().toCharArray();
    String hanyupinyin = "";
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部大写
    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
    try {
        for (int i = 0; i < cl_chars.length; i++) {
            String str = String.valueOf(cl_chars[i]);
            if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
            } else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
                hanyupinyin += cl_chars[i];
            } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母

                hanyupinyin += cl_chars[i];
            } else {// 否则不转换
            }
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        System.out.println("字符不能转成汉语拼音");
    }
    return hanyupinyin;
}
 
Example 5
Source File: PinyinUtil.java    From jeecg with Apache License 2.0 6 votes vote down vote up
/**
 * 汉字转换位汉语拼音,英文字符不变
 * 
 * @param chines
 *            汉字
 * @return 拼音
 */
public static String converterToSpell(String chines) {
	String pinyinName = "";
	char[] nameChar = chines.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 (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
		} else {
			pinyinName += nameChar[i];
		}
	}
	return pinyinName;
}
 
Example 6
Source File: ChinesUtil.java    From utils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取汉字串拼音,英文字符不变.
 *
 * @param chinese 汉字串
 * @return 汉语拼音
 */
public static String getFullSpell(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 < arr.length; i++) {
        if (arr[i] > 128) {
            try {
                pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            pybf.append(arr[i]);
        }
    }
    return pybf.toString();
}
 
Example 7
Source File: PinyinTest.java    From TinyPinyin with Apache License 2.0 6 votes vote down vote up
@Test
public void testIsChinese() throws BadHanyuPinyinOutputFormatCombination {
    char[] allChars = allChars();
    final int allCharsLength = allChars.length;
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);

    for (int i = 0; i < allCharsLength; i++) {
        char targetChar = allChars[i];
        String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(targetChar, format);
        if (pinyins != null && pinyins.length > 0) {
            // is chinese
            assertThat(Pinyin.isChinese(targetChar), is(true));
        } else {
            // not chinese
            assertThat(Pinyin.isChinese(targetChar), is(false));
        }
    }
}
 
Example 8
Source File: PingYinUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
 * 将字符串中的中文转化为拼音,其他字符不变
 *
 * @param inputString
 * @return
 */
public static String getPingYin(String inputString) {
    HanyuPinyinOutputFormat format = new
            HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);

    char[] input = inputString.trim().toCharArray();
    String output = "";

    try {
        for (int i = 0; i < input.length; i++) {
            if (java.lang.Character.toString(input[i]).
                    matches("[\\u4E00-\\u9FA5]+")) {
                String[] temp = PinyinHelper.
                        toHanyuPinyinStringArray(input[i],
                                format);
                output += temp[0];
            } else
                output += java.lang.Character.toString(
                        input[i]);
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }
    return output;
}
 
Example 9
Source File: PinyinUtils.java    From MaterialDesignDemo with MIT License 5 votes vote down vote up
/**
 * 根据传入的字符串(包含汉字),得到拼音
 * 沧晓 -> CANGXIAO
 * 沧  晓*& -> CANGXIAO
 * 沧晓f5 -> CANGXIAO
 *
 * @param str 字符串
 * @return
 */
public static String getPinyin(String str) {

    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

    StringBuilder sb = new StringBuilder();

    char[] charArray = str.toCharArray();
    for (int i = 0; i < charArray.length; i++) {
        char c = charArray[i];
        // 如果是空格, 跳过
        if (Character.isWhitespace(c)) {
            continue;
        }
        if (c >= -127 && c < 128 || !(c >= 0x4E00 && c <= 0x9FA5)) {
            // 肯定不是汉字
            sb.append(c);
        } else {
            String s = "";
            try {
                // 通过char得到拼音集合. 单 -> dan, shan 
                s = PinyinHelper.toHanyuPinyinStringArray(c, format)[0];
                sb.append(s);
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
                sb.append(s);
            }
        }
    }

    return sb.toString();
}
 
Example 10
Source File: FileUtil.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
/**
 * 汉字转拼音
 *
 * @param src
 * @return
 */
public static String getPingYin(String src) {
    char[] t1 = null;
    t1 = src.toCharArray();
    String[] t2 = new String[t1.length];
    HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
    t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    t3.setVCharType(HanyuPinyinVCharType.WITH_V);
    String t4 = "";
    int t0 = t1.length;
    try {
        for (int i = 0; i < t0; i++) {
            // 判断是否为汉字字符
            if (java.lang.Character.toString(t1[i]).matches(
                    "[\\u4E00-\\u9FA5]+")) {
                t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                t4 += t2[0];
            } else
                t4 += java.lang.Character.toString(t1[i]);
        }
        // System.out.println(t4);
        return t4;
    } catch (BadHanyuPinyinOutputFormatCombination e1) {
        e1.printStackTrace();
    }
    return t4;
}
 
Example 11
Source File: Utils.java    From WeSync with MIT License 5 votes vote down vote up
/**
 * 将汉字转换为全拼
 *
 * @param src
 * @return
 */
public static String getPingYin(String src) {

    char[] t1;
    t1 = src.toCharArray();
    String[] t2;
    HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();

    t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    t3.setVCharType(HanyuPinyinVCharType.WITH_V);
    String t4 = "";
    int t0 = t1.length;
    try {
        for (int i = 0; i < t0; i++) {
            // 判断是否为汉字字符
            if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                t4 += t2[0];
            } else {
                t4 += Character.toString(t1[i]);
            }
        }
        return t4;
    } catch (BadHanyuPinyinOutputFormatCombination e1) {
        e1.printStackTrace();
    }
    return t4;
}
 
Example 12
Source File: PinyinUtil.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 获取拼音集合
 * 
 * @author wyh
 * @param src
 * @return Set<String>
 */
public static Set<String> getPinyin(String src) {
	if (src != null && !src.trim().equalsIgnoreCase("")) {
		char[] srcChar;
		srcChar = src.toCharArray();
		// 汉语拼音格式输出类
		HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();

		// 输出设置,大小写,音标方式等
		hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

		String[][] temp = new String[src.length()][];
		for (int i = 0; i < srcChar.length; i++) {
			char c = srcChar[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) {
					e.printStackTrace();
				}
			} 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[] { "" };
			}
		}
		String[] pingyinArray = Exchange(temp);
		Set<String> pinyinSet = new HashSet<String>();
		for (int i = 0; i < pingyinArray.length; i++) {
			pinyinSet.add(pingyinArray[i]);
		}
		return pinyinSet;
	}
	return null;
}
 
Example 13
Source File: PinyinUtil.java    From apkextractor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取汉字串拼音,英文字符不变
 * @param chinese 汉字串
 * @return 汉语拼音
 */
public static String getFullSpell(String chinese) {
    StringBuilder pybf = new StringBuilder();
    char[] arr = chinese.toCharArray();
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    try{
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
            } else {
                pybf.append(arr[i]);
            }
        }
    }
    catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }
    catch(java.lang.NullPointerException npex){
        npex.printStackTrace();
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
    return pybf.toString();
}
 
Example 14
Source File: CharacterParser.java    From LoveTalkClient with Apache License 2.0 5 votes vote down vote up
public static String getPingYin(String src) {

		char[] t1 = null;
		t1 = src.toCharArray();
		String[] t2 = new String[t1.length];
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();

		t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);
		String t4 = "";
		int t0 = t1.length;
		try {
			for (int i = 0; i < t0; i++) {
				// 判断是否为汉字字符
				if (java.lang.Character.toString(t1[i]).matches(
						"[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
					t4 += t2[0];
				} else
					t4 += java.lang.Character.toString(t1[i]);
			}
			// System.out.println(t4);
			return t4;
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}
		return t4;
	}
 
Example 15
Source File: PinyinUtil.java    From apkextractor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 将字符串中的中文转化为拼音,其他字符不变
 * @param inputString
 * @return
 */
public static String getPinYin(String inputString) {
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);

    char[] input = inputString.trim().toCharArray();
    StringBuilder output = new StringBuilder();

    try {
        for (int i = 0; i < input.length; i++) {
            if (java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
                String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                output.append(temp[0]);
            } else
                output.append(input[i]);
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }
    catch(java.lang.NullPointerException npex){
        npex.printStackTrace();
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
    return output.toString();
}
 
Example 16
Source File: PinyinUtil.java    From hdw-dubbo with Apache License 2.0 5 votes vote down vote up
/**
 * 将汉字转换为全拼
 *
 * @param src
 * @return String
 */
public static final String getPinYin(String src) {
    if (src == null) {
        return "";
    }
    char[] t1 = null;
    t1 = src.toCharArray();
    String[] t2 = new String[t1.length];
    // 设置汉字拼音输出的格式
    HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
    t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    t3.setVCharType(HanyuPinyinVCharType.WITH_V);
    String t4 = "";
    int t0 = t1.length;
    try {
        for (int i = 0; i < t0; i++) {
            // 判断是否为汉字字符
            if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
                t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
            } else {
                // 如果不是汉字字符,直接取出字符并连接到字符串t4后
                t4 += Character.toString(t1[i]);
            }
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        logger.error("", e);
    }
    return t4;
}
 
Example 17
Source File: ChineseUtil.java    From vscrawler with Apache License 2.0 5 votes vote down vote up
/**
 * 汉字转换位汉语拼音,英文字符不变
 * 
 * @param chines
 *            汉字
 * @return 拼音
 */
public static String converterToSpell(String chines) {
    // long start = System.currentTimeMillis();
    StringBuilder pinyinName = new StringBuilder();
    char[] nameChar = chines.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 {
                String[] strResult = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
                if (strResult != null && strResult.length > 0) {
                    pinyinName.append(strResult[0]);
                } else {
                    pinyinName.append(nameChar[i]);
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            pinyinName.append(nameChar[i]);
        }
    }
    // long end = System.currentTimeMillis();
    // System.out.println("cost time : " + (end - start));
    if (pinyinName.toString().contains("u:")) {
        return pinyinName.toString().replaceAll("u:", "v");
    } else {
        return pinyinName.toString();
    }
}
 
Example 18
Source File: ShortTextSearcher.java    From short-text-search with Apache License 2.0 5 votes vote down vote up
public String getPinYin(String words, boolean acronym) {
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
    char[] chars = words.trim().toCharArray();
    StringBuilder result = new StringBuilder();
    int ignoredCount = 0;
    try {
        for (char c : chars) {
            if(!isChinese(c)){
                ignoredCount++;
                result.append(c);
                continue;
            }
            String[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
            if(acronym){
                result.append(pinyinStringArray[0].charAt(0));
            }else {
                String pinYin = pinyinStringArray[0].toLowerCase().replace("ü", "v");
                if(StringUtils.isBlank(pinYin)){
                    continue;
                }
                result.append(pinYin);
                charPinYin.add(pinYin);
                if(pinYin.length() > charMaxPinYinLength){
                    charMaxPinYinLength = pinYin.length();
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("获取拼音失败: "+words, e);
    }
    if(ignoredCount == chars.length){
        return null;
    }
    return result.toString().toLowerCase();
}
 
Example 19
Source File: PinyinUtil.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 将汉字转换为全拼
 * 
 * @param src
 * @return String
 */
public static String getPinYin(String src) {
	char[] t1 = null;
	t1 = src.toCharArray();
	// org.jeecgframework.core.util.LogUtil.info(t1.length);
	String[] t2 = new String[t1.length];
	// org.jeecgframework.core.util.LogUtil.info(t2.length);
	// 设置汉字拼音输出的格式
	HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
	t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
	t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	t3.setVCharType(HanyuPinyinVCharType.WITH_V);
	String t4 = "";
	int t0 = t1.length;
	try {
		for (int i = 0; i < t0; i++) {
			// 判断能否为汉字字符
			// org.jeecgframework.core.util.LogUtil.info(t1[i]);
			if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
				t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
				t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
			} else {
				// 如果不是汉字字符,间接取出字符并连接到字符串t4后
				t4 += Character.toString(t1[i]);
			}
		}
	} catch (BadHanyuPinyinOutputFormatCombination e) {
		e.printStackTrace();
	}
	return t4;
}
 
Example 20
Source File: PinyinUtil.java    From jeewx with Apache License 2.0 3 votes vote down vote up
/**
 * 
 * 将汉字转换成拼音
 * 
 * @param hanzi
 * 
 * @param separator
 * 
 * @return
 */

@SuppressWarnings("deprecation")
public static String hanziToPinyin(String hanzi, String separator) {

	// 创建汉语拼音处理类

	HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

	// 输出设置,大小写,音标方式

	defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

	defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

	String pinyingStr = "";

	try {

		pinyingStr = PinyinHelper.toHanyuPinyinString(hanzi, defaultFormat,
				separator);

	} catch (BadHanyuPinyinOutputFormatCombination e) {

		e.printStackTrace();

	}

	return pinyingStr;

}