Java converts Chinese names to PinYin

Source: Internet
Author: User

The Code is as follows:

1 import net. sourceForge. pinyin4j. pinyinhelper; 2 Import net. sourceForge. pinyin4j. format. hanyupinyincasetype; 3 Import net. sourceForge. pinyin4j. format. hanyupinyinoutputformat; 4 Import net. sourceForge. pinyin4j. format. hanyupinyintonetype; 5 import net. sourceForge. pinyin4j. format. hanyupinyinvchartype; 6 public class spellhelper {7 // convert Chinese to English 8 public static string getename (string name) 9 {10 hanyupinyinyinoutputformat pyformat = new hanyupinyinoutputformat (); 11 pyformat. setcasetype (hanyupinyincasetype. lowercase); 12 pyformat. settonetype (hanyupinyintonetype. without_tone); 13 pyformat. setvchartype (hanyupinyinvchartype. with_v); 14 15 return pinyinhelper. tohanyupinyinstring (name, pyformat, ""); 16} 17 18 // The first letter of the last name and name must be in uppercase 19 public static string getupename (string name) {20 char [] STRs = Name. tochararray (); 21 string newname = NULL; 22 23 // name length 24 if (STRs. length = 2) {25 newname = toupcase (getename ("" + STRs [0]) + "" 26 + toupcase (getename ("" + STRs [1]); 27} else if (STRs. length = 3) 28 {29 newname = toupcase (getename ("" + STRs [0]) + "" 30 + toupcase (getename ("" + STRs [1] + STRs [2]); 31} 32 else if (STRs. length = 4) 33 {34 newname = toupcase (getename ("" + STRs [0] + STRs [1]) + "" 35 + toupcase (getename ("" + STRs [2] + STRs [3]); 36} else 37 {38 newname = toupcase (getename (name )); 39} 40 return newname; 41} 42 43 // upper letter 44 Private Static string toupcase (string Str) {45 stringbuffer newstr = new stringbuffer (); 46 newstr. append (Str. substring (0, 1 )). touppercase ()). append (47 Str. substring (1, str. length (); 48 49 return newstr. tostring (); 50} 51 public static void main (string [] ARGs) {52 system. out. println (getupename ("Li Yuchun"); 53 54} 55 56}

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.