java中文转拼音

2022/4/27 9:12:36

本文主要是介绍java中文转拼音,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

阅读原文

废话不多数上代码!

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


这篇关于java中文转拼音的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程