Java gets examples of Chinese pinyin Chinese acronyms and Chinese initials

  • 2020-05-10 18:15:35
  • OfStack

We sometimes have situations where we need to get some Chinese pinyin, Chinese acronyms, and Chinese initials. Here's how to get some Chinese pinyin acronyms.

1. Project establishment and configuration

First, we establish a Java project, new libs folder and introducing a 734 a7099-4830-39 f2 - a136-0 e850ccdcc7a. jar file, this step believe you don't have to write detail, skip.

2, access to Chinese pinyin (such as: guangdong province -- > guangdongsheng)


</pre><pre name="code" class="java"><span style="white-space:pre">  </span>/** 
   *  Get a full spell in Chinese  
   * @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++) 
      { 
        //  Determine if it is a Chinese 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 acronyms (e.g. Guangdong province --) > gds)
 


  </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) 
  { 
    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 the first Chinese letter and convert it to a capital letter (e.g. Guangdong province --) > G)

We need to combine the getPinYinHeadChar method in step 3. 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 the pinyin acronym: " + s); 
    StringBuffer sb = new StringBuffer(s); 
    if (sb.length() > 1) 
    { 
      String ss = sb.delete(1, sb.length()).toString(); 
      System.out.println(" The initial letter is: " 
          + Character.toUpperCase(ss.toCharArray()[0]) + ""); 

Related articles: