这里获得汉字的拼音使用了pinyin4j这个插件,因为多音字的原因效果并不理想
1 /** 2 * 获得汉字拼音 3 * @param name 4 * @return 5 */ 6 @SuppressWarnings("deprecation") 7 public static String getPiYin(String src) { 8 HanyuPinyinOutputFormat outFormat = new HanyuPinyinOutputFormat(); 9 outFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 10 outFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 11 outFormat.setVCharType(HanyuPinyinVCharType.WITH_V); 12 try { 13 return PinyinHelper.toHanyuPinyinString(src, outFormat, ""); 14 } catch (BadHanyuPinyinOutputFormatCombination e1) { 15 e1.printStackTrace(); 16 return src; 17 } 18 } 19 20 /** 21 * 获得拼音首字母 22 * @param name 23 * @return 24 */ 25 public static String getPinYinFirstLetter(String name){ 26 char[] str=StringUtils.defaultIfEmpty(name, "").toCharArray(); 27 String shouZiMu=""; 28 for ( char string : str) { 29 shouZiMu+=getPiYin(String.valueOf(string)).charAt(0); 30 } 31 return shouZiMu; 32 } 33 34 public boolean isChinese(char c) { 35 Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); 36 if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS 37 || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS 38 || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A 39 || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION 40 || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION 41 || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { 42 return true; 43 } 44 return false; 45 } 46 47 public boolean isChinese(String s) { 48 return s.matches("[\\u4E00-\\u9FA5]+"); 49 }
时间: 2024-11-05 09:25:30