JAVA converts Chinese into Pinyin tool class

Source: Internet
Author: User

Add Pinyin4j-2.5.0.jar (included in the attachment, remember to unzip) to the Classpath.


Tool class Pinyinutils code see below:

package com.test.utils;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;public class  pinyinutils {    /**     *  to determine if a string is empty       *      *  @param  text     *              string      * @ Return     */    private static boolean isnull ( String text)  {        if  (text == null | |  text.trim (). Equals ("")                 | |   "null". Equalsignorecase (text))  {             return true;        }         return false;    }    /**      *  get Chinese character string pinyin initials, English characters invariant      *      *  @param  chineseString     *              Chinese character string      *  @return   First letter      */     public static string getfirstspell (string chinesestring)  {         stringbuffer pybf = new stringbuffer ();         if  (IsNULL (chinesestring)) &NBsp {            return null;         }        char arr =  Chinesestring.trim (). ReplaceAll (":",  ""). ReplaceAll ("(",  "")                  .replaceall (")",  ""). ToCharArray () [0];         HanyuPinyinOutputFormat defaultFormat = new  Hanyupinyinoutputformat ();         defaultformat.setcasetype ( Hanyupinyincasetype.lowercase);         defaultformat.settonetype ( Hanyupinyintonetype.without_tone);        if  (arr >  )  {            try {                 string[] _t = pinyinhelper.tohanyupinyinstringarray ( arr,                         defaultformat);                 if  (_t != null)  {                     pybf.append (_t [0].charat (0));                 }            } catch  ( Badhanyupinyinoutputformatcombination e)  {                 e.printstacktrace ();             }        } else {             pybf.append (arr);        }         string result = pybf.tostring (). ReplaceAll ("\\w",  ""). Trim ();         return result.touppercase ();    }     /**     *  get Chinese character string pinyin, English characters unchanged      *       *  @param  chineseString     *              Chinese character string      *  @return   Hanyu Pinyin      */    public static string getspell (string chinesestring)  {        StringBuffer pybf  = new stringbuffer ();        if  (IsNULL (chinesestring))  {             return null;         }        char[] arr = chinesestring.trim (). ToCharArray ();        hanyupinyinoutputformat defaultformat  = new hanyupinyinoutputformat ();         Defaultformat.setcasetype (hanyupinyincasetype.lowercase);         Defaultformat.settonetype (Hanyupinyintonetype.without_tone);         try {            for  (int i =  0; i < arr.length; i++)  {                 if  (arr[i] > 128)  {                     try {                         string[]  pinyinArray = PinyinHelper                                  .tohanyupinyinstringarray (Arr[i], defaultformat);                         if   (pinyinarray != null && pinyinarray.length > 0)  {                              string firstpinyin = pinyinarray[0];                              if  (firstpinyin != null)  {                                  pybf.append (Firstpinyin);                              }                         }                     } catch  ( Badhanyupinyinoutputformatcombination&nbSp;e)  {                         e.printstacktrace ();                     }                 } else {                     pybf.append ( Arr[i]);                 }             }             return pybf.tostring ();         } catch  (exception e)  {             e.printstacktracE ();            return  "";         }    }    /**      *  take the first letter of each word in Chinese string      *      *  @param   chinesestring     *              China      *  @return  ZG     */     public static string getallfirstspell (string chinesestring)  {         stringbuffer pybf = new stringbuffer ();         if  (IsNULL (chinesestring))  {             return null;        }      &Nbsp;  char[] arr = chinesestring.trim (). ToCharArray ();         hanyupinyinoutputformat defaultformat = new hanyupinyinoutputformat ();         defaultformat.setcasetype (hanyupinyincasetype.lowercase);         defaultformat.settonetype (Hanyupinyintonetype.without_tone);         try {             for  (int i = 0; i < arr.length; i++)  {                 if  (arr[i ] > 128)  {                     String han =  ""  + arr[i];        &nbSp;            pybf.append (GetFirstSpell (han));                 }             }             return pybf.tostring ();        }  catch  (exception e)  {             E.printstacktrace ();            return  "";         }    }    public  Static void main (String[] args)  {         System.out.println (Getfirstspell ("C"));         system.out.println ( Getallfirstspell ("China"));    }} 




This article is from the "12314480" blog, please be sure to keep this source http://12324480.blog.51cto.com/12314480/1875402

JAVA converts Chinese into Pinyin tool class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.