废话不多数上代码!
pinyin4j
引入maven依赖
<dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.1</version> </dependency>
实例
public class Client { public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); //拼音小写 format.setCaseType(HanyuPinyinCaseType.LOWERCASE); //不带声调 format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //要转换的中文,格式,转换之后的拼音的分隔符,遇到不能转换的是否保留 wo,shi,zhong,guo,ren,,hello System.out.println(PinyinHelper.toHanYuPinyinString("我是中国人,hello", format, ",", true)); } }
输出结果为
wo,shi,zhong,guo,ren,,hello