From c103bd9eba73928f118f49e72cdce3bc4bda08a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Sun, 8 Oct 2023 17:02:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=8E=B7=E5=8F=96=E6=8B=BC=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kwan/springbootkwan/utils/PinyinUtil.java | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/kwan/springbootkwan/utils/PinyinUtil.java b/src/main/java/com/kwan/springbootkwan/utils/PinyinUtil.java index 9d7ac0b..480baa8 100644 --- a/src/main/java/com/kwan/springbootkwan/utils/PinyinUtil.java +++ b/src/main/java/com/kwan/springbootkwan/utils/PinyinUtil.java @@ -7,7 +7,6 @@ import org.springframework.util.CollectionUtils; import java.util.List; - /** * 拼音工具类 * @@ -18,7 +17,7 @@ import java.util.List; public class PinyinUtil { /** - * 获取中文完整拼音 + * 获取中文完整拼音-不带音标 * * @param chineseStr * @return @@ -32,6 +31,41 @@ public class PinyinUtil { return stringBuilder.toString(); } + /** + * 获取中文完整拼音-指定分隔符 + * + * @param chineseStr + * @return + */ + public static String getPinyin(String chineseStr, String separator) { + List pinyins = PinyinDictionary.convertToPinyin(chineseStr); + StringBuilder stringBuilder = new StringBuilder(); + if (!CollectionUtils.isEmpty(pinyins)) { + final int size = pinyins.size(); + for (int i = 0; i < size; i++) { + if (i != 0 && i != size) { + stringBuilder.append(separator); + } + stringBuilder.append(pinyins.get(i).getPinyinWithoutTone()); + } + } + return stringBuilder.toString(); + } + + /** + * 带音标 + * + * @param chineseStr + * @return + */ + public static String getPinyinWithToneMark(String chineseStr) { + List pinyins = PinyinDictionary.convertToPinyin(chineseStr); + StringBuilder stringBuilder = new StringBuilder(); + for (Pinyin pinyin : pinyins) { + stringBuilder.append(pinyin.getPinyinWithToneMark()); + } + return stringBuilder.toString(); + } /** * 获取中文拼音首字母 @@ -46,4 +80,9 @@ public class PinyinUtil { } return String.valueOf(pinyins.get(0).getPinyinWithoutTone().charAt(0)); } -} + + public static void main(String[] args) { + System.out.println(PinyinUtil.getPinyin("秦英杰", "_")); + System.out.println(PinyinUtil.getInitial("秦英杰")); + } +} \ No newline at end of file -- GitLab