cn.hutool.extra.pinyin.engine.jpinyin.JPinyinEngine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.extra.pinyin.engine.jpinyin;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.extra.pinyin.PinyinEngine;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
/**
* 封装了Jpinyin的引擎。
*
*
* jpinyin(github库作者已删除)封装。
*
*
*
* 引入:
*
* <dependency>
* <groupId>com.github.stuxuhai</groupId>
* <artifactId>jpinyin</artifactId>
* <version>1.1.8</version>
* </dependency>
*
*
* @author looly
*/
public class JPinyinEngine implements PinyinEngine {
//设置汉子拼音输出的格式
PinyinFormat format;
public JPinyinEngine(){
this(null);
}
public JPinyinEngine(PinyinFormat format){
init(format);
}
public void init(PinyinFormat format){
if(null == format){
// 不加声调
format = PinyinFormat.WITHOUT_TONE;
}
this.format = format;
}
@Override
public String getPinyin(char c) {
String[] results = PinyinHelper.convertToPinyinArray(c, format);
return ArrayUtil.isEmpty(results) ? String.valueOf(c) : results[0];
}
@Override
public String getPinyin(String str, String separator) {
try {
return PinyinHelper.convertToPinyinString(str, separator, format);
} catch (PinyinException e) {
throw new cn.hutool.extra.pinyin.PinyinException(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy