com.github.jackpanz.spring.util.PinyinUtil Maven / Gradle / Ivy
The newest version!
package com.github.jackpanz.spring.util;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.apache.commons.lang3.StringUtils;
/**
* Created by Administrator on 2016/8/30.
*/
public class PinyinUtil {
public static String getFirtChar_pinyin(String str)
{
if(!StringUtils.isBlank(str.trim()))
{
return getPinYinHeadChar(str.substring(0,1));
}
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;
}
public static void main(String[] args) {
String str = "國人民";
System.out.println(getFirtChar_pinyin(str).toUpperCase());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy