Commit 94026498 by zhangxingmin

push

parent 8e18cca6
......@@ -283,18 +283,31 @@ public class ApiCsfCommonServiceImpl implements ApiCsfCommonService {
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
StringBuilder pinyin = new StringBuilder();
boolean lastIsChinese = false; // 标记上一个字符是否为汉字,用于控制空格添加
for (char c : chinese.toCharArray()) {
if (Character.toString(c).matches("[\\u4E00-\\u9FA5]")) {
try {
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
if (pinyinArray != null && pinyinArray.length > 0) {
// 如果上一个字符是汉字且当前也是汉字,则先加空格(避免开头空格)
if (lastIsChinese) {
pinyin.append(' ');
}
pinyin.append(pinyinArray[0]);
lastIsChinese = true;
} else {
// 如果没有拼音,原样追加
pinyin.append(c);
lastIsChinese = false;
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
pinyin.append(c);
lastIsChinese = false;
}
} else {
pinyin.append(c);
lastIsChinese = false;
}
}
return pinyin.toString();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment