pinyin4j use in Android

Source: Internet
Author: User

First, what is pinyin4j

Pinyin4j is a popular Java library that supports conversion between Chinese characters and pinyin. Pinyin output format can be customized.

Second, how to use?

Basic use, see my article reproduced in this can be

http://blog.csdn.net/stoppig/article/details/40889413

Iii. Use of extensions

1. Use in Android


Create a new Android project and put the PINYIN4J package into the Libs folder. Then import the appropriate class to use in the class that you want to use.

Import Android.util.log;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;

This step allows you to freely use the features in pinyin4j.

2. Use pinyin4j to generate spelling shorthand

When you use PINYIN4J, you first split the Chinese string and split it into characters. After parsing each character and generating a string array of English letters, we only need to take the No. 0-bit string of the parsed string array of each character, and combine it to get a phonetic shorthand for a Chinese string. The following is the sample code.

public static string Convertertofirstspell (String chines) {if (Chines = = null) {LOG.E (TAG, "string value = null, return!"); Return "";} String pinyinname = ""; try {char[] Namechar = Chines.tochararray (); getinstance (). Setcasetype ( Hanyupinyincasetype.uppercase); getinstance (). Settonetype (Hanyupinyintonetype.without_tone); for (int i = 0; i < Namechar.length; i++) {if (Namechar[i] >) {try {pinyinname + pinyinhelper.tohanyupinyinstringarray (Namechar[i], getinstance ()) [0 ].charat (0);} catch (Badhanyupinyinoutputformatcombination e) {log.e (TAG, E + "");}} else {pinyinname + = Namechar[i];}}} catch (Exception e) {log.e (TAG, E + "");p Inyinname = "";} return pinyinname;}

These are some of the tutorials used by pinyin4j in Android, the following is a sample code download link:

Sample code

pinyin4j use in Android

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.