From 10f02bb9bc12a285ae8c90da42dd6dee3781dfd7 Mon Sep 17 00:00:00 2001 From: choweli <1030848819@qq.com> Date: Thu, 27 Feb 2025 10:27:46 +0800 Subject: [PATCH] =?UTF-8?q?PinyinUtil=E5=A2=9E=E5=8A=A0=E9=80=89=E6=8B=A9?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E8=BF=94=E5=9B=9E=E5=A3=B0=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../engine/pinyin4j/Pinyin4jEngine.java | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/hutool-extra/src/main/java/cn/hutool/extra/pinyin/engine/pinyin4j/Pinyin4jEngine.java b/hutool-extra/src/main/java/cn/hutool/extra/pinyin/engine/pinyin4j/Pinyin4jEngine.java index b30a05eef..de332ea4a 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/pinyin/engine/pinyin4j/Pinyin4jEngine.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/pinyin/engine/pinyin4j/Pinyin4jEngine.java @@ -86,16 +86,15 @@ public class Pinyin4jEngine implements PinyinEngine { public String getPinyin(char c, boolean tone) { String result; if(tone){ - //增加声调 - HanyuPinyinOutputFormat formatTemp = new HanyuPinyinOutputFormat(); - // 小写 - formatTemp.setCaseType(HanyuPinyinCaseType.LOWERCASE); - // 加声调 - formatTemp.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); - // - formatTemp.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); - try { + //增加声调 + HanyuPinyinOutputFormat formatTemp = new HanyuPinyinOutputFormat(); + // 小写 + formatTemp.setCaseType(HanyuPinyinCaseType.LOWERCASE); + // 加声调 + formatTemp.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); + // + formatTemp.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); String[] results = PinyinHelper.toHanyuPinyinStringArray(c, formatTemp); result = ArrayUtil.isEmpty(results) ? String.valueOf(c) : results[0]; } catch (BadHanyuPinyinOutputFormatCombination e) { @@ -136,17 +135,8 @@ public class Pinyin4jEngine implements PinyinEngine { @Override public String getPinyin(String str, String separator, boolean tone) { - final StrBuilder result = StrUtil.strBuilder(); if(tone){ - //增加声调 - HanyuPinyinOutputFormat formatTemp = new HanyuPinyinOutputFormat(); - // 小写 - formatTemp.setCaseType(HanyuPinyinCaseType.LOWERCASE); - // 加声调 - formatTemp.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); - // - formatTemp.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); - + final StrBuilder result = StrUtil.strBuilder(); boolean isFirst = true; final int strLen = str.length(); try { @@ -156,6 +146,14 @@ public class Pinyin4jEngine implements PinyinEngine { } else{ result.append(separator); } + //增加声调 + HanyuPinyinOutputFormat formatTemp = new HanyuPinyinOutputFormat(); + // 小写 + formatTemp.setCaseType(HanyuPinyinCaseType.LOWERCASE); + // 加声调 + formatTemp.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); + // + formatTemp.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); final String[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(str.charAt(i), formatTemp); if(ArrayUtil.isEmpty(pinyinStringArray)){ result.append(str.charAt(i)); @@ -166,10 +164,9 @@ public class Pinyin4jEngine implements PinyinEngine { } catch (BadHanyuPinyinOutputFormatCombination e) { throw new PinyinException(e); } + return result.toString(); }else { - result.append(getPinyin(str, separator)); + return getPinyin(str, separator); } - return result.toString(); - } }