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