Convert kanji to pinyin in Java

Source: Internet
Author: User

First, add the Pinyin4j-2.5.0.jar package, the jar package in the source code has

Write Tool class

public class pingyinutil {      /**       *  convert Chinese in string to pinyin, other characters unchanged       *        *  @param  inputString      *  @return        */      public static string getpingyin (String  inputstring)  {          HanyuPinyinOutputFormat  Format = new hanyupinyinoutputformat ();           format.setcasetype (hanyupinyincasetype.lowercase);           format.settonetype (Hanyupinyintonetype.without_tone);           format.setvchartype (HANYUPINYINVCHARTYPE.WITH_V);               char[] input = inputstring.trim (). ToCharArray ();           String output =  "";              try {               for  (int i = 0; i < input.length; i++)  {                   if   (Java.lang.Character.toString (Input[i]). Matches ("[\\u4e00-\\u9fa5]+"))  {                        String[] temp = pinyinhelper.tohanyupinyinstringarray (Input[i], format);                       &NBSP;OUTPUT&NBSP;+=&NBSP;TEMP[0];                   }  else                       output += java.lang.character.tostring (Input[i]);               }           } catch  (badhanyupinyinoutputformatcombination e)  {               e.printstacktrace ();           }          return  output;      }      /**         *  get Chinese character string pinyin initials, English characters invariant         * @ param chinese  HanStrings         *  @return   initials          */        public static String  Getfirstspell (String chinese)  {                  stringbuffer pybf = new stringbuffer ();                  char[] arr  = chinese.tochararray ();                  HanyuPinyinOutputFormat defaultFormat = new  Hanyupinyinoutputformat ();                  defaultformat.setcasetype (hanyupinyincasetype.lowercase);                  defaultformat.settonetype (Hanyupinyintonetype.without_tone);                  for  (int i = 0;  i < arr.length; i++)  {                          if  (arr[i ] > 128)  {                                   try {                                           String[] temp =  Pinyinhelper.tohanyupinyinstringarray (Arr[i], defaulTformat);                                           if  (temp != null)  {                                                    pybf.append (Temp[0].charat (0));                                            }                                  } catch  ( Badhanyupinyinoutputformatcombination e)  {                                           e.printstacktrace ();                                   }                           } else {                                   pybf.append (Arr[i]); &NBSP;&NBSP;&Nbsp;                       }                  }                  return pybf.tostring (). ReplaceAll ("\\w",  ""). Trim ();          }         /**         *  get Chinese character string pinyin, English characters invariant         *  @param  chinese  Chinese character string         *  @return   Hanyu Pinyin          */        public static  String getfullspell (String chinese)  {               &nBsp;  stringbuffer pybf = new stringbuffer ();                  char[] arr =  Chinese.tochararray ();                  hanyupinyinoutputformat defaultformat = new hanyupinyinoutputformat ();                   Defaultformat.setcasetype (hanyupinyincasetype.lowercase);                  defaultformat.settonetype (HanyuPinyinToneType.WITHOUT_ TONE);                  for  (int i = 0; i < arr.length; i++)  {                          if  (arr[i] >  )  {                                  try  {                                           pybf.append (Pinyinhelper.tohanyupinyinstringarray (arr[i],  DefaultFormat) [0]);                                  }  catch  (badhanyupinyinoutputformatcombination e)  {                                           e.printstacktrace ();                                   }                          }  else {                                   Pybf.append (Arr[i]);                          }                  }                  return pybf.tostring ();          }     }

Then you can just tune the interface.

public class Test {public static void main (string[] args) {String str= "kanji to pinyin";          System.out.println ("formerly:" +str);      System.out.println ("Converted to:" +pingyinutil.getpingyin ("Kanji to Pinyin")); }  }

Run results

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/82/76/wKioL1dWQEyB6isuAAAfc11AsZc664.png-wh_500x0-wm_3 -wmp_4-s_3263637081.png "title=" 20160601101539049.png "alt=" Wkiol1dwqeyb6isuaaafc11aszc664.png-wh_50 "/>

Source Address: http://down.51cto.com/data/2221969

This article is from the "Android Development column" blog, be sure to keep this source http://liuyvhao.blog.51cto.com/11690759/1786868

Convert Chinese characters to pinyin in Java

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.