Java Chinese character to PinYin pinyin4j, java Chinese Character pinyin4j

Source: Internet
Author: User

Java Chinese character to PinYin pinyin4j, java Chinese Character pinyin4j

Package com. joyce. pinyin4j; 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;/*** PinYin4j * @ author Joyce. luo * @ date 01:04:38 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */public class PinyinUtil {public static void main (String [] args) {String str1 = PinyinUtil. toPinyinString ('zhang'); System. out. println ("chinese char -->" + str1); String str2 = PinyinUtil. toPinyinString ('C'); System. out. println ("english char -->" + str2); String str3 = PinyinUtil. toPinyinString ("Zhang San"); System. out. println ("chinese string -->" + str3); Stri Ng str4 = PinyinUtil. toPinyinString ("Hello World"); System. out. println ("english string -->" + str4); String str5 = PinyinUtil. toPinyinString ("Hi Michael, hello world! "); System. out. println ("chinese and english -->" + str5);}/*** get the Pinyin output format * @ return Pinyin output format * @ author Joyce. luo * @ date 01:40:10 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */private static HanyuPinyinOutputFormat getPinyinFormat () {HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat ();/** UPPERCASE: upper case (ZHONG) * LOWERCASE: lower case (zhong) */format. setCaseType (HanyuPinyinCaseType. LOWERCASE);/** WITHOUT_TONE: No phonetic symbol (zhong) * WITH_TONE_NUMBER: 1-4 digits indicating the English logo (zhong4) * WITH_TONE_MARK: directly using the phonetic symbol (must WITH_U_UNICODE or exception) (zh ò ng) */format. setToneType (HanyuPinyinToneType. WITHOUT_TONE);/** WITH_V: Use v to represent u (nv) * WITH_U_AND_COLON: Use "u:" To represent u (nu :) * WITH_U_UNICODE: use u (n u) directly) */format. setVCharType (HanyuPinyinVCharType. WITH_U_UNICODE); // return the output format return format;}/*** convert the character to Pinyin *. If the character is an English character, the output is directly * If the character is a Polyphonic word, take the first * @ param c character * @ return converted string * @ author Joyce. luo * @ date 01:34:55 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */public static String toPinyinString (char c) {HanyuPinyinOutputFormat format = PinyinUtil. getpinyiterator (); try {String [] pinyin = PinyinHelper. toHanyuPinyinStringArray (c, format); if (null = pinyin | pinyin. length <1) {return String. valueOf (c);} return pinyin [0];} catch (Exception e) {e. printStackTrace (); return null ;}/ *** string converted to Pinyin * @ param str string to be converted * @ return converted string * @ author Joyce. luo * @ date 01:38:17 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */public static String toPinyinString (String str) {if (null = str | "". equals (str) {return null;} StringBuilder sb = new StringBuilder (); String tempPinyin = null; for (int I = 0; I <str. length (); I ++) {tempPinyin = PinyinUtil. toPinyinString (str. charAt (I); sb. append (tempPinyin);} return sb. toString ();}}


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.