TinyPinyin是一个适用于Java和Android的快速、低内存占用的汉字转拼音库。
文档
- https://github.com/promeG/TinyPinyin
- https://mvnrepository.com/artifact/com.github.promeg/tinypinyin
引入依赖
<!-- TinyPinyin核心包 -->
<dependency>
<groupId>com.github.promeg</groupId>
<artifactId>tinypinyin</artifactId>
<version>2.0.3</version>
</dependency>
<!-- 可选,适用于Java的中国地区词典 -->
<dependency>
<groupId>com.github.promeg</groupId>
<artifactId>tinypinyin-lexicons-java-cncity</artifactId>
<version>2.0.3</version>
</dependency>
方法签名
/**
* 如果c为汉字,则返回大写拼音;如果c不是汉字,则返回String.valueOf(c)
*/
String Pinyin.toPinyin(char c)
/**
* c为汉字,则返回true,否则返回false
*/
boolean Pinyin.isChinese(char c)
/**
* 将输入字符串转为拼音,转换过程中会使用之前设置的用户词典,以字符为单位插入分隔符
*/
String toPinyin(String str, String separator)
代码示例
package com.example.demo;
import com.github.promeg.pinyinhelper.Pinyin;
import org.junit.Test;
public class PinyinTests {
@Test
public void testPinyin(){
// 返回大写拼音
String pinyin = Pinyin.toPinyin("你好中国", "");
System.out.println(pinyin);
// NIHAOZHONGGUO
}
}