先决条件:

pinyin4j.jar(Pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换。拼音输出格式可以定制。)

下载地址:http://pan.baidu.com/share/link?shareid=3958741959&uk=3792676205

PinyinUtil.java

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 inputStr
* @return 汉字拼音
*/
public static String getPinYin(String inputStr) {
HanyuPinyinOutputFormat hpFormat = new HanyuPinyinOutputFormat();
hpFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写
hpFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
hpFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inputStr.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], hpFormat);
output += temp[0];
} else {
output += input[i];
}
}
} catch (Exception 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);
defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V); 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));
} else {
pybf.append(arr[i]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
}
}
return pybf.toString().replaceAll("\\W", "").trim();
} /**
* 获取汉字串拼音,英文字符不变
*
* @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);
defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V); 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();
}
return pybf.toString();
}

test.java

public class test {
public static void main(String[] args) {
String result = getPinYin("赠送");
System.out.println(result);
}
}

java 汉字转拼音的更多相关文章

  1. java汉字转拼音以及得到首字母通用方法

    package oa.common.utils;   import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.piny ...

  2. [转]Java汉字按照拼音排序

    最近项目上使用到汉字排序的问题,网上搜索了一下后普遍使用下面的方法比较. @Test public void test_sort_pinyin() { Collator cmp = Collator. ...

  3. java汉字转拼音的工具类

    import com.google.common.base.Strings;import net.sourceforge.pinyin4j.PinyinHelper;import net.source ...

  4. Java汉字转拼音

    import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...

  5. Android Java汉字转拼音总结

    转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/23187701 开发过程中有时候会遇到使用拼音模糊搜索等功能(典型的就是Andro ...

  6. JAVA汉字转拼音(取首字母大写)

    import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.pinyin4j.format.HanyuPinyinCaseT ...

  7. 用jpinyin实现汉字转拼音功能

    一.简介 项目地址:https://github.com/stuxuhai/jpinyin JPinyin是一个汉字转拼音的Java开源类库,在PinYin4j的功能基础上做了一些改进. [JPiny ...

  8. 汉字转拼音开源工具包Jpinyin介绍

    最近要实现一个根据词语得到词语对应拼音的功能,找到了Jpinyin这个开源工具包,使用下来发现它非常强大,完全满足我的需求,下面对它做一个简单的介绍,希望能够帮助到有需要的朋友. https://gi ...

  9. java实现将汉字转为拼音

    有时候为了方便操作程序的开发,需要将汉字转为拼音等操作.下面这个是自己结合网上的资料,加上自己在公司项目中的亲自实践.完整的实现了将汉字转为拼音的操作.这个Demo只是负责将其转换,在main方法中测 ...

随机推荐

  1. BT Smart vs ANT+ 技术孰优孰劣?

    自从Bluetooth SIG提出Bluetooth 4.0,其BLE(Bluetooth Low Energy)开始用于穿戴式电子,而后也用于iBeacon室内定位,更之后Bluetooth 4.1 ...

  2. poj2636---Electrical Outlets(插线板)

    #include <stdio.h> #include <stdlib.h> int main() { int n,nc,i; scanf("%d",&am ...

  3. Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性

    新的keyword auto C++11中引入auto第一种作用是为了自己主动类型推导 auto的自己主动类型推导,用于从初始化表达式中判断出变量的数据类型.通过auto的自己主动类型推导.能够大大简 ...

  4. Javascript 获取窗口的大小和位置

    在Javascript中可以使用OuterWidth,OuterHeight 获取浏览器的大小.用 innerWidth,innerHeight 来获取窗口的大小(除去浏览器边框部分).对于IE6 及 ...

  5. 关于使用由CA机构(EJBCA)颁发的证书实现SLLSocket双向认证服务端报null cert chain的解决方案

    在 SSLSocket实现服务端和客户端双向认证的例子 文章中最后提到使用keytool.exe的自签证书实现双向认证可以,但是使用ejbca生成证书实现SLL Socket的双向认证是服务端老是报错 ...

  6. android Fragment相关概念简介

    Fragment 详细介绍连接:http://blog.csdn.net/harvic880925/article/details/44927375 fragment是一种控制器对象,activity ...

  7. Centos 7 安装Mono和Jexus 默认目录安装 (一)

    一.准备环境 yum -y install gcc gcc-c++ bison pkgconfig glib2-devel gettext make libpng-devel libjpeg-deve ...

  8. 对discuz的代码分析学习(三)mysql驱动

    一. 属性分析         1.tablepre :表名称前缀        2.version : 数据库版本        3.drivertype:驱动类型        4.querynu ...

  9. 百度ueditor富文本编辑器的使用

    百度ueditor富文本编辑器的使用 //以下为我在官网下载的ueditor v1.3.5 php版的大楷配置步骤第一步: //配置文件的引入应该比功能文件先引入,最后设置语言类型.即:editor. ...

  10. BlueTooth的EDR是什么

    EDR 即Enhanced data rate,是蓝牙技术中增强速率的缩写,其特色是大大提高了蓝牙技术的数据传输速率,达到了2.1Mbps ,是目前蓝牙技术的三倍.因此除了可获得更稳定的音频流传送的更 ...