前提是导包hanyu4j
pom.xml引入
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
@Test
public void test() throws BadHanyuPinyinOutputFormatCombination {
//定义一个字符串
String s = "这是测试字符串";
//转成字符数组
char[] chars = s.toCharArray();
//导入pinyin4j,并设置属性
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
for(Integer i =0;i<chars.length;i++){
//将字符转为拼音,需要用一个数组接收
String[] strings = PinyinHelper.toHanyuPinyinStringArray(chars[i], t3);
System.out.println(strings[0]);
}
}
控制台输出的是
不用谢!