获取汉字串拼音首字母以及获取汉字串拼音
引入包:pinyin4j-2.5.0.jar 即可使用.
package org.wcy.util;
import java.io.UnsupportedEncodingException;
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 PinyinToolkit {
/**
* 获取汉字串拼音首字母,英文字符不变
*
* @param chinese
* 汉字串
* @return 汉语拼音首字母
*/
public static String cn2FirstSpell(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[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i],
defaultFormat);
if (_t != null) {
pybf.append(_t[0].charAt(0));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString().replaceAll("\\W", "").trim();
}
/**
* 获取汉字串拼音,英文字符不变
*
* @param chinese
* 汉字串
* @return 汉语拼音
*/
public static String cn2Spell(String chinese,String split) {
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++) {
try {
if(isChinese(arr[i]))
pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i],
defaultFormat)[0]).append(split);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
}
return pybf.toString();
}
public static void main(String[] args) throws UnsupportedEncodingException {
String x = "数据库技术";
System.out.println(cn2FirstSpell(x));
System.out.println(cn2Spell(x,","));
}
/**
* 判断是否为汉字
* @param c
* @return
*/
private static boolean isChinese(char c) {
if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+"))
return true;
return false;
}
}
分享到:
相关推荐
java中根据汉字字符串获取拼音首字母工具类
oracle获取汉字拼音拼音首字母的功能函数 oracle 获取汉字拼音
VB获取汉字的拼音首字母 支持26000多汉字 可以自己添加汉字库 自动保存没能获取的汉字
PHP获取字符串拼音首字母 包含多音字扩展
delphi 源代码 软件 汉字拼音首字母 将汉字转为拼音首字母,获取取汉字拼音首字母,delphi 源代码 软件 汉字拼音首字母
网上有现成的提取拼音首字母程序,但我查到的版本都不支持多音字提取,以前用过一个JS的支持多音字提取的函数,就把PHP版本和JS版本综合了一下,写了个类,希望对开发者有用。
可以直接调用方法,有需要配合IndexBar-master这个通讯录开源控件使用的可以自己去下载IndexBar这个控件
使用MySQL函数,实现获取汉字拼音首字母。在SQL语句中传入汉字返回对应的首字母
获取汉字的拼音首字母,可以把一段汉字生成拼音首字母的字符串
用PB写的获取汉字拼音首字母函数,也就是汉字拼音的声母.
支持2万+汉字转拼音首字母
这是我搜索网络资源,然后自己整理的一个输入汉字转拼音及获取首字母的一个示例程序,希望能帮助到兄弟姐妹们。
java提取汉字拼音首字母的代码,简体中文的编码范围从B0A1(45217)一直到F7FE(63486),不支持多音字处理
java 取出汉语字符串各汉字拼音首字母并大写
获得汉字字符串拼音首字母
本文实例讲述了JS实现获取汉字首字母拼音、全拼音及混拼音的方法。分享给大家供大家参考,具体如下: 这里需要用到一个js获取汉字拼音的插件,可点击此处本站下载。 运行效果如下: 完整示例代码: <!DOCTYPE ...
本程序用C#所写,用来获取汉字拼音首字母。
oracle获取汉字拼音首字母,用于根据首字母筛选汉字
获取汉字汉语拼音首字母,UITableView中文也可以加index了
sql 汉字转拼音首字母 代码 function --汉字转拼音首字母sql Create FUNCTION dbo.fnpbGetPYFirstLetter ( @str NVARCHAR(4000) )