<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
引入pinyin4j依赖
直接上Util类
/**
* @author shangxichen
* @date 2020-03-13
*/
public class PinyinUtil {
private static Map<String, String> map = new HashMap<>();
private static final Logger logger= LoggerFactory.getLogger(PinyinUtil.class);
private static void initMap() {
try (
InputStream is = PinyinUtil.class.getClassLoader().getResourceAsStream("cn_en_punc.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
) {
String keyword;
while ((keyword = reader.readLine()) != null) {
String[] temp = keyword.split(" ");
map.put(temp[0], temp[1]);
}
} catch (IOExc

本文介绍了如何在Java中使用Pinyin4j库将汉字转换为拼音首字母,并处理多音字问题。通过引入Pinyin4j依赖,可以方便地实现字符串中汉字到拼音的转换,特别适用于需要进行汉语拼音处理的场景。
&spm=1001.2101.3001.5002&articleId=104849386&d=1&t=3&u=510b24c43ec948ebbf216015ea233a60)
805

被折叠的 条评论
为什么被折叠?



