Java 之中文转拼音


1、jar: pinyin4j-2.5.0.jar 


2、代码

package com.java.util;

import java.util.HashSet;
import java.util.Set;

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 pinyin4j {
 
 /**
  * 字符串集合转换字符串(逗号分隔)
  * @author wyh
  * @param stringSet
  * @return
  */
 public static String makeStringByStringSet(Set stringSet){
  StringBuilder str = new StringBuilder();
  int i=0;
  for(String s : stringSet){
   if(i == stringSet.size() - 1){
    str.append(s);
   }else{
    str.append(s + ",");
   }
   i++;
  }
  return str.toString().toLowerCase();
 }
 
 /**
  * 获取拼音集合
  * @author wyh
  * @param src
  * @return Set
  */
 public static Set getPinyin(String src){
  if(src!=null && !src.trim().equalsIgnoreCase("")){
   char[] srcChar ;
   srcChar=src.toCharArray();
   //汉语拼音格式输出类
   HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();

//输出设置,大小写,音标方式等
   hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 
   hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
   hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
   
   String[][] temp = new String[src.length()][];
   for(int i=0;i=65 && (int)c<=90) || ((int)c>=97 && (int)c<=122)){
     temp[i] = new String[]{String.valueOf(srcChar[i])};
    }else{
     temp[i] = new String[]{""};
    }
   }
   String[] pingyinArray = Exchange(temp);
   Set pinyinSet = new HashSet();
   for(int i=0;i= 2){           
            int len1 = strJaggedArray[0].length;
            int len2 = strJaggedArray[1].length;
            int newlen = len1*len2;
            String[] temp = new String[newlen];
            int Index = 0;
            for(int i=0;i


你可能感兴趣的:(Java)