Home > Java > javaTutorial > body text

Introduction to how to obtain the first letter of Chinese Pinyin in Java

黄舟
Release: 2017-09-20 09:49:46
Original
2147 people have browsed it

The following editor will bring you an example of obtaining the first letter of Chinese Pinyin in Java. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

is as follows:


import net.sourceforge.pinyin4j.PinyinHelper;

public class PinyinHelperUtil {

 /**
  * 得到中文首字母(中国 -> ZG)
  * @param str 需要转化的中文字符串
  * @return 大写首字母缩写的字符串
  */
 public static String getPinYinHeadChar(String str) {
  StringBuilder convert = new StringBuilder();
  for (int j = 0; j < str.length(); j++) {
   char word = str.charAt(j);
   String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
   if (pinyinArray != null) {
    convert.append(pinyinArray[0].charAt(0));
   } else {
    convert.append(word);
   }
  }
  return convert.toString().toUpperCase();
 }
 
}
Copy after login

The above is the detailed content of Introduction to how to obtain the first letter of Chinese Pinyin in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template