Java 中文转换拼音工具

/**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved.</p>
* <p> Created on 19941115</p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.alpaca.framework.utils.chinese; 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; /**
* @Package:cn.ucaner.framework.utils
* @ClassName:PinyinUtils
* @Description: <p> 中文转换拼音工具</p>
* @Author: -
* @CreatTime:2017年8月30日 下午2:09:07
* @Modify By:
* @ModifyTime:
* @Modify marker:
* @version V1.0
*/
public class PinyinUtils { /**
* 获取汉字串拼音首字母,英文字符不变
* @param chinese 汉字串
* @return 汉语拼音首字母
*/
public static String getChineseFirstWord(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[] word = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (word != null) {
pybf.append(word[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 getChineseAllWord(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 {
pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString();
} /**
* For Test By Jason
*/
public static void main(String[] args) {
//issue: 不能空格 woaizhongguo! byJson
System.out.println(getChineseAllWord("我爱中国! byJson"));
System.out.println(getChineseFirstWord("我爱中国! byJson"));//wazgbyJson
}
}

  

Java 中文转换拼音工具的更多相关文章

  1. NPinyin 中文转换拼音代码

    Mono 3.2 测试NPinyin 中文转换拼音代码   C#中文转换为拼音NPinyin代码  在Mono 3.2下运行正常,Spacebuilder 有使用到NPinyin组件,代码兼容性没有问 ...

  2. java 中文转拼音之pinyin4j

    一.简介 有时候,须要将汉字编程相应的拼音.以方便数据的处理.比方在Android手机应用的开发上.要查询联系人的姓名.通常都是用拼音进行查询的. 比方要查询"曹孟德",就能够输入 ...

  3. Mono 3.2 测试NPinyin 中文转换拼音代码

    C#中文转换为拼音NPinyin代码  在Mono 3.2下运行正常,Spacebuilder 有使用到NPinyin组件,代码兼容性没有问题. using System; using System. ...

  4. 在QML 中用javascritpt 将中文转换拼音,可以在音标

    项目需要, 今天整理了一下.在QML调用javascrit将中文汉字转换成拼音. 感觉执行效率低.下面是主要代码. 具体代码请参考QMLPinyin 代码 ```import "./piny ...

  5. java 中文转换成Unicode编码和Unicode编码转换成中文

    转自:一叶飘舟 http://blog.csdn.net/jdsjlzx/article/details/ package lia.meetlucene; import java.io.IOExcep ...

  6. PHP中文转拼音扩展

    Pinyin 基于 CC-CEDICT 词典的中文转拼音工具,更准确的支持多音字的汉字转拼音解决方案. 安装 使用 Composer 安装: $ composer require "over ...

  7. C#中文转换为拼音NPinyin代码【转】

    项目地址:https://code.google.com/p/npinyin/ 在一个采集的程序中,为了给每个文章起一个别名,据说有很好的别名的话,对于百度.google的收录 是很有好处的.按照Se ...

  8. java util - 中文、繁体转成拼音工具pinyin4j

    需要 pinyin4j-2.5.0.jar 包 代码例子 package cn.java.pinyin4j; import net.sourceforge.pinyin4j.PinyinHelper; ...

  9. java PinYinUtils 拼音工具类

    package com.sicdt.library.core.utils; import java.util.HashSet; import java.util.Set; import net.sou ...

随机推荐

  1. 使用electron开发桌面级小程序自动部署系统

    那一天我二十一岁,在我一生的黄金时代,我有好多奢望.我想爱,想吃,还想在一瞬间变成天上半明半暗的云,后来我才知道,生活就是个缓慢受锤的过程,人一天天老下去,奢望也一天天消逝,最后变得像挨了锤的牛一样. ...

  2. SQL on Hadoop技术综述

    一.系统架构 runtime framework v.s. mpp 在SQL on Hadoop系统中,有两种架构: 1.一种是基于某个运行时框架来构建查询引擎,典型案例是Hive: 2.另一种是仿照 ...

  3. leetcode 494. 目标数

    题目描述: 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在你有两个符号 + 和 -.对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面. 返回可以 ...

  4. 关于用maven创建的springboot工程打包jar后找不到配置文件的问题

    你的resources文件夹的名称写错了!!! 如果不在pom中配置build的资源路径,那么你的资源文件名必须默认是“resources”! 解决办法有两个: 1.修改文件夹名称 2.添加pom的b ...

  5. SpringBoot——配置文件加载位置及外部配置加载顺序

    声明 本文部分转自:SpringBoot配置文件加载位置与优先级 正文 1. 项目内部配置文件 spring boot 启动会扫描以下位置的application.properties或者applic ...

  6. tensorflow 预训练模型列表

    tensorflow 预训练模型列表 https://github.com/tensorflow/models/tree/master/research/slim Pre-trained Models ...

  7. 图片上传: ajax-formdata-upload

    传送门:https://www.cnblogs.com/qiumingcheng/p/6854933.html ajax-formdata-upload.html <!DOCTYPE html& ...

  8. 【Node.js】Node.js的安装

    Node.js的简介 简单的说,Node.js 是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件 ...

  9. springmvc@RequestMapping-params参数规则

    springmvc@RequestMapping-params参数规则 https://blog.csdn.net/qq_33439525/article/details/78193190 版权声明: ...

  10. Deploy a plain HTTP registry

    当docker pull a.b.com/ubuntu:14.04的时候,如果提示:“Docker x509 insecure registry”之类的,说这个registry不安全的,如果就是想在a ...