Java汉字转成汉语拼音工具类,需要用到pinyin4j.jar包.

 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 HanyuPinyinHelper { /**
* 将文字转为汉语拼音
* @param chineselanguage 要转成拼音的中文
*/
public String toHanyuPinyin(String ChineseLanguage){
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;
try {
for (int i=0; i<cl_chars.length; i++){
if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")){// 如果字符是中文,则将中文转为汉语拼音
hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
} else {// 如果字符不是中文,则不转换
hanyupinyin += cl_chars[i];
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
System.out.println("字符不能转成汉语拼音");
}
return hanyupinyin;
} public static String getFirstLettersUp(String ChineseLanguage){
return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.UPPERCASE);
} public static String getFirstLettersLo(String ChineseLanguage){
return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.LOWERCASE);
} public static String getFirstLetters(String ChineseLanguage,HanyuPinyinCaseType caseType) {
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(caseType);// 输出拼音全部大写
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
try {
for (int i = 0; i < cl_chars.length; i++) {
String str = String.valueOf(cl_chars[i]);
if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0].substring(0, 1);
} else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
hanyupinyin += cl_chars[i];
} else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母
hanyupinyin += cl_chars[i];
} else {// 否则不转换
hanyupinyin += cl_chars[i];//如果是标点符号的话,带着
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
System.out.println("字符不能转成汉语拼音");
}
return hanyupinyin;
} public static String getPinyinString(String ChineseLanguage){
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部大写
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
try {
for (int i = 0; i < cl_chars.length; i++) {
String str = String.valueOf(cl_chars[i]);
if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(
cl_chars[i], defaultFormat)[0];
} else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
hanyupinyin += cl_chars[i];
} else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母 hanyupinyin += cl_chars[i];
} else {// 否则不转换
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
System.out.println("字符不能转成汉语拼音");
}
return hanyupinyin;
}
/**
* 取第一个汉字的第一个字符
* @Title: getFirstLetter
* @Description: TODO
* @return String
* @throws
*/
public static String getFirstLetter(String ChineseLanguage){
char[] cl_chars = ChineseLanguage.trim().toCharArray();
String hanyupinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);// 输出拼音全部大写
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
try {
String str = String.valueOf(cl_chars[0]);
if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
hanyupinyin = PinyinHelper.toHanyuPinyinStringArray(
cl_chars[0], defaultFormat)[0].substring(0, 1);;
} else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
hanyupinyin += cl_chars[0];
} else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母 hanyupinyin += cl_chars[0];
} else {// 否则不转换 }
} catch (BadHanyuPinyinOutputFormatCombination e) {
System.out.println("字符不能转成汉语拼音");
}
return hanyupinyin;
} public static void main(String[] args) {
HanyuPinyinHelper hanyuPinyinHelper = new HanyuPinyinHelper() ;
System.out.println(hanyuPinyinHelper.toHanyuPinyin("多发的发独守空房阿道夫打发第三方"));
}
}

Java汉字转成汉语拼音工具类的更多相关文章

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

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

  2. java中excel导入\导出工具类

    1.导入工具 package com.linrain.jcs.test; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import ...

  3. java中定义一个CloneUtil 工具类

    其实所有的java对象都可以具备克隆能力,只是因为在基础类Object中被设定成了一个保留方法(protected),要想真正拥有克隆的能力, 就需要实现Cloneable接口,重写clone方法.通 ...

  4. java中的Arrays这个工具类你真的会用吗

    Java源码系列三-工具类Arrays ​ 今天分享java的源码的第三弹,Arrays这个工具类的源码.因为近期在复习数据结构,了解到Arrays里面的排序算法和二分查找等的实现,收益匪浅,决定研读 ...

  5. Java操作文件夹的工具类

    Java操作文件夹的工具类 import java.io.File; public class DeleteDirectory { /** * 删除单个文件 * @param fileName 要删除 ...

  6. java代码行数统计工具类

    package com.syl.demo.test; import java.io.*; /** * java代码行数统计工具类 * Created by 孙义朗 on 2017/11/17 0017 ...

  7. Java加载Properties配置文件工具类

    Java加载Properties配置文件工具类 import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; ...

  8. Java 操作Redis封装RedisTemplate工具类

    package com.example.redisdistlock.util; import org.springframework.beans.factory.annotation.Autowire ...

  9. java后台表单验证工具类

    /** * 描述 java后台表单验证工具类 * * @ClassName ValidationUtil * @Author wzf * @DATE 2018/10/27 15:21 * @VerSi ...

随机推荐

  1. Java多线程编程之同步器

    同步器 为每种特定的同步问题提供了解决方案 Semaphore Semaphore[信号标:旗语],通过计数器控制对共享资源的访问. 测试类: package concurrent; import c ...

  2. 第二百七十七天 how can I 坚持

    开玩笑要有个度,哎,或许这就是缘分,很容易受别人影响吗? 中国人为什么会经常抱怨,不抱怨,挺好. 睡觉,红颜祸水,老婆是要能一起 生活的,不是失去,是上天在帮我,哈哈.

  3. 最大连续子数和问题-homework-03

    一.说明 这次的作业做的不好,一小点怨念ing····· 首先向TA说明下,我的小伙伴“丢下”我后我不知道,以至于发现剩下我一个的时间有点晚,我机智地找到了一个3个人的小组,又叫到了一个小伙伴,但是悲 ...

  4. 修改Oracle 10g Express Edition默认的8080端口

    http://sucre.blog.51cto.com/1084905/475531 ————————————————————————————————————————————————————————— ...

  5. 提高iOS开发效率的方法和工具

    http://www.cocoachina.com/ios/20150717/12626.html 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先 ...

  6. POJ 3668 Game of Lines (暴力,判重)

    题意:给定 n 个点,每个点都可以和另一个点相连,问你共有多少种不同斜率的直线. 析:那就直接暴力好了,反正数也不大,用set判重就好,注意斜率不存在的情况. 代码如下: #include <c ...

  7. Elasticsearch和mysql数据同步(elasticsearch-jdbc)

    1.介绍 对mysql.oracle等数据库数据进行同步到ES有三种做法:一个是通过elasticsearch提供的API进行增删改查,一个就是通过中间件进行数据全量.增量的数据同步,另一个是通过收集 ...

  8. 使用SVG Path绘图

    最近一个项目,需要做个Web版本的设计器,用来进行工厂流水线布局的设计. 项目中采用了SVG.JS来做,但是以前流水线是采用单纯的画线的方式实现.客户提出希望用不同的底纹表示不同的流水线,经过一番调查 ...

  9. How to Be Good at Mathematics

    How to Be Good at Mathematics Community Q&A Sometimes, the hardest subject for some people is ma ...

  10. PostgreSQL的 initdb 源代码分析之二十三

    继续分析: vacuum_db(); 展开: cmd是:/home/pgsql/project/bin/postgres" --single -F -O -c search_path=pg_ ...