Java Chinese Pinyin Tool class __java

Source: Internet
Author: User

Java Kanji Pinyin Class. Using PINYIN4J to implement

Package com.pinyin4j;
Import Net.sourceforge.pinyin4j.PinyinHelper;
Import Net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
Import Net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;

Import Net.sourceforge.pinyin4j.format.HanyuPinyinToneType; /** * Chinese Pinyin tools <br> * pinyin4j * * * @author Jayden * * * * */public class Pinyinutil {private static Pinyinut

	Il instance;
		public static Pinyinutil getinstance () {if (instance = null) {instance = new Pinyinutil ();
	return instance;

	Private Hanyupinyinoutputformat OutputFormat = null; Private Hanyupinyinoutputformat GetOutputFormat () {if (OutputFormat = null) {OutputFormat = new Hanyupinyinoutputf
			Ormat ();
			Outputformat.settonetype (Hanyupinyintonetype.without_tone);
		Outputformat.setcasetype (hanyupinyincasetype.lowercase);
	return OutputFormat; /** * Gets all characters in the string * * @param str * @return * @throws Exception/public string getstringinitial (Strin G str) {StringBuffer SBF = new StringBuffer ();
			if (str!= null) {for (int i = 0; i < str.length (); i++) {Sbf.append getcharinitial (Str.charat (i)));
	} return sbf.tostring (); /** * Get Chinese character First letter * * @param c * @return * @throws Exception/public String getcharinitial (char c) {T
			ry {string[] STRs = Pinyinhelper.tohanyupinyinstringarray (c, GetOutputFormat ());
			String initial = "";
				if (STRs!= null && strs.length > 0) {String str = strs[0];
				if (str!= null && str.length () > 0) {initial + = Str.charat (0);
		} return initial;
			catch (Exception ex) {ex.printstacktrace ();
		return C + "";
		} public string Getstringpinyin (string chines) {char[] Namechar = Chines.tochararray ();
		String pinyinstr = "";
				for (int i = 0; i < namechar.length; i++) {try {char cha=namechar[i]; if (Namechar[i] > 128) {pinyinstr = Pinyinhelper.tohanyupinyinstringarray (Cha, GetOutputFormat ()) [0];
				} catch (Exception ex) {ex.printstacktrace ();
			Pinyinstr + = Namechar[i];
	} return PINYINSTR; /** * Acquire character Fu Quan spell * * @param c * @return/public String Getcharpinyin (char c) {try {string[] STRs = P
			Inyinhelper.tohanyupinyinstringarray (c, GetOutputFormat ());
			String str = "";
			if (STRs!= null && strs.length > 0) {str = strs[0];
		return str;
			catch (Exception ex) {ex.printstacktrace ();
		return C + ""; } public static void Main (string[] args) {String stringinitial = Pinyinutil.getinstance (). Getstringinitial ("Yue is an uncommon word.
		The general phonetic code cannot parse ");

		System.out.println (stringinitial);
		String Stringpinyin = Pinyinutil.getinstance (). Getstringpinyin ("Yue is an obscure word general phonetic code cannot parse");

	System.out.println (Stringpinyin); }
}


 

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.