乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      使用pinyin4j將中文轉(zhuǎn)換為漢語拼音

       躍來躍去 2009-02-16
      前言:以前師傅跟我說~如果別人有寫好的現(xiàn)成的東西就不要自己瞎琢磨寫了,除非是有特殊的需要,要不很難寫的比別人的好。別人的是經(jīng)過很多人驗證過的,必然能適應(yīng)大眾的需要,你自己寫一個又耽誤時間又不一定效率高?,F(xiàn)在看確實是。之前自己找了半天的如果將中文轉(zhuǎn)換為漢語拼音的現(xiàn)成的工具類~到頭來還是直接用pinyin4j來的方便。
      pinyin4j官方網(wǎng)站:http://pinyin4j./
      直接去下載最新的jar包在項目中引用就可以使用了。下面放出一個寫好的例子。將中文轉(zhuǎn)換為漢語拼音。生僻的字基本也沒問題,很好用。
       
       
      import net.sourceforge.pinyin4j.PinyinHelper;
      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;
      /**
       * 使用pinyin4j將中文轉(zhuǎn)換為漢語拼音
       * @author hanxiaoyue
       */
      public class CnToPinYin {
        
       public static String getPinYinStr(String cnstr) {
        StringBuilder sb = new StringBuilder();
        HanyuPinyinOutputFormat PINYIN_FORMAT = new HanyuPinyinOutputFormat();
        //返回的字符串中去除音調(diào)標(biāo)記
        PINYIN_FORMAT.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        //發(fā)音“驢(lu->lv)”的那個韻母用v表示
        PINYIN_FORMAT.setVCharType(HanyuPinyinVCharType.WITH_V);
        for(int i = 0; i < cnstr.length(); i++) {
         char c = cnstr.charAt(i);
         if(c <= 255) {
          sb.append(c);
         } else {
          String pinyin = null;
          try {
           String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c,PINYIN_FORMAT);
           pinyin = pinyinArray[0];
          } catch (BadHanyuPinyinOutputFormatCombination e) {
           e.getMessage();
          } catch (NullPointerException e) {
           // 如果是日文,可能拋出該異常
          }
          if (pinyin != null) {
           sb.append(pinyin);
          }
         }
        }
        //System.out.println(sb.toString()); 
        return sb.toString();
       }
       
         public static void main(String[] args) {
          String cnstr = "";
          StringBuilder sb = new StringBuilder();
          HanyuPinyinOutputFormat PINYIN_FORMAT = new HanyuPinyinOutputFormat();
          PINYIN_FORMAT.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
          PINYIN_FORMAT.setVCharType(HanyuPinyinVCharType.WITH_V);
          for (int i = 0; i < cnstr.length(); i++) {
           char c = cnstr.charAt(i);
           if (c <= 255) {
            sb.append(c);
           } else {
            String pinyin = null;
            try {
             String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c,PINYIN_FORMAT);
             pinyin = pinyinArray[0];
            } catch (BadHanyuPinyinOutputFormatCombination e) {
             e.getMessage();
            } catch (NullPointerException e) {
           // 如果是日文,可能拋出該異常
            }
            if (pinyin != null) {
             sb.append(pinyin);
            }
           }
          }
          System.out.println(sb.toString());
         }
      }

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多