java-汉语转拼音
来源:互联网 发布:首届全球程序员节 编辑:程序博客网 时间:2024/05/06 15:03
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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
* 中文转拼音工具类
*/
public class ChineseConvertPinyinUtil {
/**
* 将中文转换为全拼
* @param src
* @return
*/
public static String getPinYin(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;
if(t0==0){
return "-";//默认一个字符
}
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]);
}
}
return t4;
} catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
return t4;
}
/**
* 将字符串中的,转为中文的首字母
*/
public static String getPinYinHeadChar(String str) {
StringBuffer convert = new StringBuffer();
for (int j = 0; j < str.length(); j++) {
char word = str.charAt(j);
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert.append(pinyinArray[0].charAt(0));
}else {
convert.append(word);
}
}
return convert.toString();
}
/**
* 首个字符为中文,返回中文首字母,否则返回空
*/
public static String getPinYinInital(String str) {
str = str.trim();
StringBuffer convert = new StringBuffer();
if(str.length()>0){
char word = str.charAt(0);
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert.append(pinyinArray[0].charAt(0));
}
}
return convert.toString();
}
//测试
public static void main(String[] args) {
System.out.println(getPinYin("编程"));
System.out.println(getPinYinHeadChar("编程"));
System.out.println(getPinYinInital("编程"));
}
}
0 0
- java 汉语转拼音
- Java 汉语转拼音
- java-汉语转拼音
- java汉语转拼音
- java将汉语转拼音
- java将汉语转拼音
- java将汉语转拼音
- 汉语转拼音工具
- 汉语转拼音缩写
- 汉语转拼音pinyin4j
- Android 汉语转拼音
- 汉语转拼音
- java中将汉语转成拼音
- C#中汉语转拼音
- C#中汉语转拼音
- js汉语转拼音方法
- 【python】xpinyin汉语转拼音
- java实现将汉语转换为拼音
- C++ sqlite数据库增删改查(基于sqlite3)
- HDU4462 Scaring the Birds
- 海哥说的未来趋势、我最近遇到的问题
- 图片比较工具这几款比较好
- php自带加密解密函数
- java-汉语转拼音
- java.lang.NoClassDefFoundError:
- 数据结构实验之求二叉树后序遍历和层次遍历
- hibernate多列关联
- unity 入门环境 1
- C# SQL 多条件查询技巧
- 微信开发那些事
- 收集整理的非常有用的PHP函数
- CentOS 7 安装.NET Core