mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-02 11:49:07 +08:00
fix pinyin
This commit is contained in:
parent
3bca95c34a
commit
63ccb9b0eb
@ -48,6 +48,6 @@ public interface PinyinEngine {
|
||||
default String getFirstLetter(String str, String separator) {
|
||||
final String splitSeparator = StrUtil.isEmpty(separator) ? "#" : separator;
|
||||
final String[] split = StrUtil.split(getPinyin(str, splitSeparator), splitSeparator);
|
||||
return ArrayUtil.join(split, separator, (s)->String.valueOf(s.charAt(0)));
|
||||
return ArrayUtil.join(split, separator, (s)->String.valueOf(s.length() > 0 ? s.charAt(0) : ""));
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.hutool.extra.pinyin.engine.pinyin4j;
|
||||
|
||||
import cn.hutool.core.text.StrBuilder;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.pinyin.PinyinEngine;
|
||||
@ -83,16 +84,27 @@ public class Pinyin4jEngine implements PinyinEngine {
|
||||
|
||||
@Override
|
||||
public String getPinyin(String str, String separator) {
|
||||
// 添加一个后缀是为了解决Pinyin4j的bug,在指定分隔符后,最后两个词的分隔符失效
|
||||
str += StrUtil.SPACE;
|
||||
String result;
|
||||
final StrBuilder result = StrUtil.strBuilder();
|
||||
boolean isFirst = true;
|
||||
final int strLen = str.length();
|
||||
try {
|
||||
result = PinyinHelper.toHanYuPinyinString(str, format, separator, true);
|
||||
for(int i = 0; i < strLen; i++){
|
||||
if(isFirst){
|
||||
isFirst = false;
|
||||
} else{
|
||||
result.append(separator);
|
||||
}
|
||||
final String[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(str.charAt(i), format);
|
||||
if(ArrayUtil.isEmpty(pinyinStringArray)){
|
||||
result.append(str.charAt(i));
|
||||
} else{
|
||||
result.append(pinyinStringArray[0]);
|
||||
}
|
||||
}
|
||||
} catch (BadHanyuPinyinOutputFormatCombination e) {
|
||||
throw new PinyinException(e);
|
||||
}
|
||||
|
||||
return StrUtil.removeSuffix(result, StrUtil.SPACE);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -13,10 +13,10 @@ public class PinyinUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPinyinByPinyin4jTest(){
|
||||
public void getPinyinByPinyin4jTest() {
|
||||
final Pinyin4jEngine engine = new Pinyin4jEngine();
|
||||
final String pinyin = engine.getPinyin("你好", " ");
|
||||
Assert.assertEquals("ni hao", pinyin);
|
||||
final String pinyin = engine.getPinyin("你好h", " ");
|
||||
Assert.assertEquals("ni hao h", pinyin);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user