Java gets examples of Chinese pinyin, Chinese initials, and Chinese first letter _java

Source: Internet
Author: User
Tags stringbuffer

We sometimes encounter situations where we need to get some Chinese pinyin, Chinese initials and Chinese initials, and I'll show you how to get the abbreviation for Chinese pinyin.

1. Project establishment and configuration

First, we build a Java project, create a new Libs folder and introduce a 734a7099-4830-39f2-a136-0e850ccdcc7a.jar file, which I believe will not be written in detail, skipped.

2, to obtain Chinese pinyin (such as: Guangdong province-->guangdongsheng)

</pre><pre name= "code" class= "java" ><span style= "White-space:pre" > </span>/** * Get all Chinese spelling * 
    @param src need to convert the Chinese string * @return/public static string Getpingyin (String src) {char[] T1 = null; 
    T1 = Src.tochararray (); 
    string[] t2 = new String[t1.length]; 
    hanyupinyinoutputformat t3 = new Hanyupinyinoutputformat (); 
    T3.setcasetype (hanyupinyincasetype.lowercase); 
    T3.settonetype (Hanyupinyintonetype.without_tone); 
    T3.setvchartype (HANYUPINYINVCHARTYPE.WITH_V); 
    String t4 = ""; 
    int t0 = t1.length; try {for (int i = 0; i < t0 i++) {//To determine if it is a kanji character if (java.lang.Character.toString 
          (T1[i]). Matches ("[\\u4e00-\\u9fa5]+")) {t2 = Pinyinhelper.tohanyupinyinstringarray (t1[i], T3); 
        T4 + = t2[0]; 
        else {t4 + = java.lang.Character.toString (T1[i]); 
    } return T4; catch (BADHANYUPINYINOUTPUTformatcombination E1) {e1.printstacktrace (); 
  return t4; 
 }

3. Get Chinese initials (eg:-->gds, Guangdong Province)

  </pre><pre name= "code" class= "java" ><span style= "White-space:pre" >  </span>/** 
   * Get the Chinese initials 
   * @param str need to convert the Chinese string 
   * 
   @return 
  /public static string Getpinyinheadchar (String str) 
  { C8/>string convert = ""; 
    for (int j = 0; J < Str.length (); j + +) 
    { 
      char word = Str.charat (j); 
      string[] Pinyinarray = Pinyinhelper.tohanyupinyinstringarray (word); 
      if (Pinyinarray!= null) 
      { 
        convert + = Pinyinarray[0].charat (0); 
      } else 
      { 
        convert + + word; 
      } 
    } 
    return convert; 
  } 

4. Get Chinese initials and convert to uppercase letters (e.g. Guangdong province--> G)

We need to combine steps 3 Getpinyinheadchar method, the code is as follows:

</pre><pre name= "code" class= "java" ><span style= "White-space:pre" >    </span>string s = Getpinyinheadchar ("Guangdong province"); 
    System.out.println ("Get Pinyin initials as:" + s); 
    StringBuffer sb = new StringBuffer (s); 
    if (Sb.length () > 1) 
    { 
      String ss = Sb.delete (1, Sb.length ()). ToString (); 
      System.out.println ("The first letter obtained is:" 
          + character.touppercase (ss.tochararray () [0]) + ""); 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.