package com.junfun.pms;
import net.sourceforge.pinyin4j.PinyinHelper;
public class PinyinUtils {
public static String getFirstLetter(String input) {
StringBuilder result = new StringBuilder();
for (char c : input.toCharArray()) {
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
if (pinyinArray != null && pinyinArray.length > 0) {
result.append(pinyinArray[0].charAt(0));
} else {
result.append(c);
}
}
return result.toString().toUpperCase();
}
public static void main(String[] args) {
String input = "切记一定是拼音";
String firstLetters = getFirstLetter(input);
System.out.println("拼音首字母:" + firstLetters);
}
}
标签:pinyinArray,java,拼音,首字母,result,input,public,String
From: https://blog.csdn.net/weixin_42759398/article/details/139292837