依赖架包:pinyin4j.jar

package net.jeeshop.core.util;

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 PinYinUtil {
/**
* 将字符串中的中文转化为拼音,其他字符不变
*
* @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;
}
/**
* 获取汉字串拼音首字母,英文字符不变
* @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();
} public static void main(String[] args) {
String chs = "我是中国人阿三的发生! I'm Chinese!";
System.out.println(chs);
System.out.println(getPingYin(chs));
System.out.println(getFirstSpell(chs));
}
}

  

java 中文转化为拼音的更多相关文章

  1. C#——中文转化成拼音

    在KS系统中用到了中文转化成拼音的功能.通过查阅资料为下面是代码. /// <summary> /// MyConvert 的摘要说明 /// </summary> publi ...

  2. Java 中文转换拼音工具

    Java 中文转换拼音工具 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p&g ...

  3. Java 中文乱码问题总结

    开发java应用出现乱码是很常见的,毕竟现在unicode的使用还不是很广泛,在使用gb2312(包含了gbk简体,big5繁体)的系统中要正确 实现中文的display和数据库的存储是最基本的要求. ...

  4. js中实现中文按字母拼音排序

    js中实现中文按字母拼音排序 var Pinyin = (function (){ var Pinyin = function (ops){ this.initialize(ops); }, opti ...

  5. [pinyin4j] java版汉字转换拼音(大小写)

    pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2. ...

  6. c#把汉字转化成全拼音函数(全拼)

    /// <summary>        /// 把汉字转换成拼音(全拼)        /// </summary>        /// <param name=&q ...

  7. 将汉字转化为拼音的js插件

    /*---------------------------------------------------------------- // 文件名:chinese2pinyin.js // 文件功能描 ...

  8. 简单测试--C#实现中文汉字转拼音首字母

    第一种: 这个是自己写的比较简单的实现方法,要做汉字转拼音首字母,首先应该有一个存储首字母的数组,然后将要转拼音码的汉字与每个首字母开头的第一个汉字即“最小”的汉字作比较,这里的最小指的是按拼音规则比 ...

  9. java实现汉字转为拼音

    java实现汉字转为拼音: 1.需要导入pinyin4j.jar package com.loo.pinyin; import net.sourceforge.pinyin4j.PinyinHelpe ...

随机推荐

  1. web.config设置和取值

    博客园中有一篇文章对web.config的结构做了很详细的介绍,原文见 http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.htm ...

  2. android 代码优化

    http://android.tgbus.com/Android/androidnews/200812/172247.shtml http://blog.163.com/jzq_520/blog/st ...

  3. Yosemite系统怎么录制 iOS8设备屏幕

    我一年前一直想要的一个功能,发布时很想用.一直没找到 ,很巧的是今天被测试发现了. 感谢CCTV.自己在这里也记录下: 你好!    在 OS X Yosemite  系统中,QuickTime 支持 ...

  4. js的异常捕获

    try{ ...some code... }catch(e){ ...some code... //处理错误 throw(e.name); //抛出异常 }finally{<BR> // ...

  5. android studio 2.2.2下fragment的创建和跳转

    一,首先,Fragment是android应用中十分重要的一个功能,十分轻量化,也类似于activity一样,是一个个布局,可以相互跳转和传递参数.但是,它运行起来十分流畅,而且易于管理,下面是在学习 ...

  6. C++关于Condition Variable

    #include <condition_variable> #include <mutex> #include <future> #include <iost ...

  7. WPF与WinForm开发有什么区别?

    转自http://hi.baidu.com/leoliu83/blog/item/1d1a4a66dcb41134aa184cfd.html WPF开发于WinForm之后,从技术发展的角度,WPF比 ...

  8. 【转】C#线程同步示例

    using System; using System.Threading; // 银行帐户类 class Account { int balance;                         ...

  9. Xor && 线性基练习

    #include <cstdio> #include <cstring> ; ; int cnt,Ans,b,x,n; inline int Max(int x,int y) ...

  10. Draw_extend使用OpenGL显示数据点

    //alter load_map.dev //safety verion 2016/1/12 #include <iostream> #include <fstream> #i ...