pinyin4J一款非常好用的汉字和拼音转换的插件

       给大家介绍一款非常好用的汉字转拼音,进行用拼音检索相关数据的时候比较方便,就是pinyin4j插件,在eclipse里如果是maven项目,直接导入相关约束,然后导入工具类,随时根据需求调用相关方法即可,在下面工具类代码里可以找到相应方法的具体代码,而且相关jar包可以在网上搜索pinyin4j进行下载

约束:

https://mvnrepository.com/artifact/com.belerweb/pinyin4j-->

       <dependency>

          <groupId>com.belerwebgroupId>

           <artifactId>pinyin4jartifactId>

          <version>2.5.0version>

       dependency

工具类代码:

import java.util.Arrays;

 

importnet.sourceforge.pinyin4j.PinyinHelper;

importnet.sourceforge.pinyin4j.format.HanyuPinyinCaseType;

import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;

importnet.sourceforge.pinyin4j.format.HanyuPinyinToneType;

importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

 

public class PinYin4jUtils {

      /**

       * 将字符串转换成拼音数组

       *

       * @param src

       * @return

       */

      publicstatic String[] stringToPinyin(String src) {

           returnstringToPinyin(src, false, null);

      }

 

      /**

       * 将字符串转换成拼音数组

       *

       * @param src

       * @return

       */

      publicstatic String[] stringToPinyin(String src, String separator) {

 

           returnstringToPinyin(src, true, separator);

      }

 

      /**

       * 将字符串转换成拼音数组

       *

       * @param src

       * @param isPolyphone

       *           是否查出多音字的所有拼音

       * @param separator

       *           多音字拼音之间的分隔符

       * @return

       */

      publicstatic String[] stringToPinyin(String src, boolean isPolyphone,

                 String separator) {

           //判断字符串是否为空

           if("".equals(src) || null == src) {

                 returnnull;

           }

           char[]srcChar = src.toCharArray();

           intsrcCount = srcChar.length;

           String[]srcStr = new String[srcCount];

 

           for(int i = 0; i < srcCount; i++) {

                 srcStr[i]= charToPinyin(srcChar[i], isPolyphone, separator);

           }

           returnsrcStr;

      }

 

      /**

       * 将单个字符转换成拼音

       *

       * @param src

       * @return

       */

      publicstatic String charToPinyin(char src, boolean isPolyphone,

                 String separator) {

           //创建汉语拼音处理类

           HanyuPinyinOutputFormatdefaultFormat = new HanyuPinyinOutputFormat();

           // 输出设置,大小写,音标方式

           defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

           defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

 

           StringBuffer tempPinying = newStringBuffer();

 

           //如果是中文

           if(src > 128) {

                 try{

                      //转换得出结果

                      String[]strs = PinyinHelper.toHanyuPinyinStringArray(src,

                                  defaultFormat);

 

                      //是否查出多音字,默认是查出多音字的第一个字符

                      if(isPolyphone && null != separator) {

                            for(int i = 0; i < strs.length; i++) {

                                  tempPinying.append(strs[i]);

                                  if(strs.length != (i + 1)) {

                                       //多音字之间用特殊符号间隔起来

                                       tempPinying.append(separator);

                                  }

                            }

                      }else {

                            tempPinying.append(strs[0]);

                      }

 

                 }catch (BadHanyuPinyinOutputFormatCombination e) {

                      e.printStackTrace();

                 }

           }else {

                 tempPinying.append(src);

           }

 

           returntempPinying.toString();

 

      }

 

      publicstatic String hanziToPinyin(String hanzi) {

           returnhanziToPinyin(hanzi, " ");

      }

 

      /**

       * 将汉字转换成拼音

       *

       * @param hanzi

       * @param separator

       * @return

       */

      publicstatic String hanziToPinyin(String hanzi, String separator) {

 

           //创建汉语拼音处理类

           HanyuPinyinOutputFormatdefaultFormat = new HanyuPinyinOutputFormat();

           // 输出设置,大小写,音标方式

           defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

           defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

 

           StringpinyingStr = "";

           try{

                 pinyingStr= PinyinHelper.toHanyuPinyinString(hanzi, defaultFormat,

                            separator);

           }catch (BadHanyuPinyinOutputFormatCombination e) {

                 // TODO Auto-generated catchblock

                 e.printStackTrace();

           }

           returnpinyingStr;

      }

 

      /**

       * 将字符串数组转换成字符串

       *

       * @param str

       * @param separator

       *           各个字符串之间的分隔符

       * @return

       */

      publicstatic String stringArrayToString(String[] str, String separator) {

           StringBuffersb = new StringBuffer();

           for(int i = 0; i < str.length; i++) {

                 sb.append(str[i]);

                 if(str.length != (i + 1)) {

                      sb.append(separator);

                 }

           }

           returnsb.toString();

      }

 

      /**

       * 简单的将各个字符数组之间连接起来

       *

       * @param str

       * @return

       */

      publicstatic String stringArrayToString(String[] str) {

           returnstringArrayToString(str, "");

      }

 

      /**

       * 将字符数组转换成字符串

       *

       * @param str

       * @param separator

       *           各个字符串之间的分隔符

       * @return

       */

      publicstatic String charArrayToString(char[] ch, String separator) {

           StringBuffersb = new StringBuffer();

           for(int i = 0; i < ch.length; i++) {

                 sb.append(ch[i]);

                 if(ch.length != (i + 1)) {

                      sb.append(separator);

                 }

           }

           returnsb.toString();

      }

 

      /**

       * 将字符数组转换成字符串

       *

       * @param str

       * @return

       */

      publicstatic String charArrayToString(char[] ch) {

           returncharArrayToString(ch, " ");

      }

 

      /**

       * 取汉字的首字母

       *

       * @param src

       * @param isCapital

       *           是否是大写

       * @return

       */

      publicstatic char[] getHeadByChar(char src, boolean isCapital) {

           //如果不是汉字直接返回

           if(src <= 128) {

                 returnnew char[] { src };

           }

           //获取所有的拼音

           String[]pinyingStr = PinyinHelper.toHanyuPinyinStringArray(src);

 

           //创建返回对象

           intpolyphoneSize = pinyingStr.length;

           char[]headChars = new char[polyphoneSize];

           inti = 0;

           //截取首字符

           for(String s : pinyingStr) {

                 charheadChar = s.charAt(0);

                 //首字母是否大写,默认是小写

                 if(isCapital) {

                      headChars[i]= Character.toUpperCase(headChar);

                 }else {

                      headChars[i]= headChar;

                 }

                 i++;

           }

 

           returnheadChars;

      }

 

      /**

       * 取汉字的首字母(默认是大写)

       *

       * @param src

       * @return

       */

      publicstatic char[] getHeadByChar(char src) {

           returngetHeadByChar(src, true);

      }

 

      /**

       * 查找字符串首字母

       *

       * @param src

       * @return

       */

      publicstatic String[] getHeadByString(String src) {

           returngetHeadByString(src, true);

      }

 

      /**

       * 查找字符串首字母

       *

       * @param src

       * @param isCapital

       *           是否大写

       * @return

       */

      publicstatic String[] getHeadByString(String src, boolean isCapital) {

           returngetHeadByString(src, isCapital, null);

      }

 

      /**

       * 查找字符串首字母

       *

       * @param src

       * @param isCapital

       *           是否大写

       * @param separator

       *           分隔符

       * @return

       */

      publicstatic String[] getHeadByString(String src, boolean isCapital,

                 String separator) {

           char[]chars = src.toCharArray();

           String[]headString = new String[chars.length];

           inti = 0;

           for(char ch : chars) {

 

                 char[]chs = getHeadByChar(ch, isCapital);

                 StringBuffersb = new StringBuffer();

                 if(null != separator) {

                      intj = 1;

 

                      for(char ch1 : chs) {

                            sb.append(ch1);

                            if(j != chs.length) {

                                  sb.append(separator);

                            }

                            j++;

                      }

                 }else {

                      sb.append(chs[0]);

                 }

                 headString[i]= sb.toString();

                 i++;

           }

           returnheadString;

      }

     

      publicstatic void main(String[] args) {

           //pin4j 简码 和 城市编码

           Strings1 = "好好学习天天向上";

           String[]headArray = getHeadByString(s1); // 获得每个汉字拼音首字母

           System.out.println(Arrays.toString(headArray));

          

           Strings2 ="你好" ;

           System.out.println(Arrays.toString(stringToPinyin(s2,true,",")));

          

           Strings3 ="长";

           System.out.println(Arrays.toString(stringToPinyin(s3,true,",")));

      }

}

你可能感兴趣的:(实用工具)