苏州建站公司速找苏州聚尚网络,网站建设中 即将上线html5源代码,wordpress页面标题标签,个人网站创意Java获取汉字的首字母#xff0c;例如#xff1a;中国香港#xff0c;则返回ZGXG#xff1b;Tom 中国欢迎你#xff0c;则返回 TOM ZGHYN#xff0c;如果为英文#xff0c;则返回英文的大写形式#xff0c;传空字符串则什么也不返回。 其中需要引用的maven依赖#xf…Java获取汉字的首字母例如中国香港则返回ZGXGTom 中国欢迎你则返回 TOM ZGHYN如果为英文则返回英文的大写形式传空字符串则什么也不返回。 其中需要引用的maven依赖 dependencygroupIdcom.belerweb/groupIdartifactIdpinyin4j/artifactIdversion2.5.0/version/dependency 源代码如下
import net.sourceforge.pinyin4j.PinyinHelper;public class PinYinUtil {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.toUpperCase();}
} 若有其他需求请自行修改代码。