汉字转换为拼音的相关工具

xiaoxiao2026-05-24  0

获取汉字的拼音码。如:输入 博客 返回: bo ke 不再多废话了。 请看代码。 附件里面是完整代码。 /** * 返回给定字符的汉语拼音,非汉字返回null * @param c * @return */ public static String get(char c) { int i = PinyinResource.find(c); if (i >= 0 && i < pinyinset.length) { return pinyinset[i]; } return null; } /** * 取得字符串中第一个字符的编码;如果第一个字符是汉字则返回其拼音编码的首字母(大写),否则如果第一个字符是小写英文字母则返回其大写形式;否则返回第一个字符 * @param str * @return */ public static char getInitial(String str) { if (str == null || str.length() < 1) return 0x00; char fc = str.charAt(0); if (0x3007 == fc || (0x4e00 <= fc && fc <= 0x9fa5)) { String py = get(fc); if (py != null) { fc = py.charAt(0); return (char) (fc - 32); } } else if ('a' <= fc && fc <= 'z') { return (char) (fc - 32); } return fc; } /** * 取得字符串中第一个汉字的拼音编码的首字母(大写);不存在汉字或者找不到汉字的拼音编码则返回 0x00 * @param str * @return */ public static char getChineseInitial(String str) { if (str == null || str.length() < 1) return 0x00; for (int i = 0; i < str.length(); i++) { char fc = str.charAt(i); String py = get(fc); if (py != null) { fc = py.charAt(0); return (char) (fc - 32); } } return 0x00; } /** * * @param fc * @return */ public static boolean isChinese(char fc) { return 0x3007 == fc || (0x4e00 <= fc && fc <= 0x9fa5); } /** * 取得字符串的汉语拼音表示。非汉字部分仍然保留;汉语拼音之间用指定字符串分隔。 * @param str * @param split * @return */ public static String get(String str, String split) { StringBuilder sb = new StringBuilder(); boolean lastchin = true; for (int i = 0; i < str.length(); i++) { String spy = get(str.charAt(i)); if (spy == null) { sb.append(str.charAt(i)); lastchin = false; } else { if (!lastchin) { sb.append(split); } sb.append(spy); sb.append(split); lastchin = true; } } return sb.toString(); } /** * 取得字符串的汉语拼音表示。非汉字部分仍然保留;汉语拼音之间用空格分隔。 * @param str * @return */ public static String get(String str) { return get(str, " "); } 相关资源:拼音汉字五笔转换工具
转载请注明原文地址: https://www.6miu.com/read-5049283.html

最新回复(0)