Translate Java text into pinyin

Source: Internet
Author: User

Package Com.jframe.kit;

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;

/**
* Chinese character conversion bit Hanyu Pinyin, English characters unchanged
* @author Xuke
*
*/
public class Cn2spellkit {

/**
* Chinese characters converted to initials, English characters unchanged
* @param chines Kanji
* @return Pinyin
*/
public static string Convertertofirstspell (String chines) {
String pinyinname = "";
char[] Namechar = Chines.tochararray ();
Hanyupinyinoutputformat DefaultFormat = new Hanyupinyinoutputformat ();
Defaultformat.setcasetype (hanyupinyincasetype.lowercase);
Defaultformat.settonetype (Hanyupinyintonetype.without_tone);
for (int i = 0; i < namechar.length; i++) {
if (Namechar[i] > 128) {
try {
Pinyinname + = Pinyinhelper.tohanyupinyinstringarray (Namechar[i], DefaultFormat) [0].charat (0);
} catch (Badhanyupinyinoutputformatcombination e) {
E.printstacktrace ();
}
}else{
Pinyinname + = Namechar[i];
}
}
return pinyinname;
}

/**
* Chinese character conversion bit Hanyu Pinyin, English characters unchanged
* @param chines Kanji
* @return Pinyin
*/
public static string Convertertospell (String chines) {
String pinyinname = "";
char[] Namechar = Chines.tochararray ();
Hanyupinyinoutputformat DefaultFormat = new Hanyupinyinoutputformat ();
Defaultformat.setcasetype (hanyupinyincasetype.lowercase);
Defaultformat.settonetype (Hanyupinyintonetype.without_tone);
for (int i = 0; i < namechar.length; i++) {
if (Namechar[i] > 128) {
try {
Pinyinname + = Pinyinhelper.tohanyupinyinstringarray (Namechar[i], DefaultFormat) [0];
} catch (Badhanyupinyinoutputformatcombination e) {
E.printstacktrace ();
}
}else{
Pinyinname + = Namechar[i];
}
}
return pinyinname;
}

public static string Spell (string name) {
String pinyinname = "";
if (name!=null &&! "". Equals (name)) {
if (Name.length () <=2) {
Pinyinname = Convertertospell (name);
}else{
Pinyinname = Convertertospell (name.substring (0,1)) +convertertofirstspell (Name.substring (1,name.length ()));
}
}
return pinyinname;
}

public static void Main (string[] args) {

System.out.println (Spell ("Yang Cheng"));
System.out.println (Spell ("Wang Shuai (small)"). toUpperCase ());
}
}

Must Pinyin4j-2.5.0.jar

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Translate Java text into pinyin

Related Article

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.