1 安装pinyin4j
引入Maven依赖包
<!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j --> <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>
2 使用pinyin4j将汉字转换成拼音
2.1 导入依赖项
import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.*; import net.sourceforge.pinyin4j.format.exception.*;
2.2 将汉字转换为拼音
String[] pinyins = PinyinHelper.toHanyuPinyinStringArray("天行健,君子以自强不息。");