Java 汉字获取拼音或首字母工具类代码分析
程序员文章站
2022-08-07 19:21:10
本文主要介绍java中,将字符串中的中文转化为拼音,获取汉字串拼音首字母,获取汉字串拼音的工具类,以及相关的示例代码。1、maven依赖配置(pom.xml)...
本文主要介绍java中,将字符串中的中文转化为拼音,获取汉字串拼音首字母,获取汉字串拼音的工具类,以及相关的示例代码。
1、maven依赖配置(pom.xml)
<dependency> <groupid>com.belerweb</groupid> <artifactid>pinyin4j</artifactid> <version>2.5.1</version> </dependency>
2、工具类代码
package com.cjavapy.utils.util; import net.sourceforge.pinyin4j.pinyinhelper; import net.sourceforge.pinyin4j.format.hanyupinyincasetype; import net.sourceforge.pinyin4j.format.hanyupinyinoutputformat; import net.sourceforge.pinyin4j.format.hanyupinyintonetype; import net.sourceforge.pinyin4j.format.hanyupinyinvchartype; import net.sourceforge.pinyin4j.format.exception.badhanyupinyinoutputformatcombination; /** * 拼音工具类 */ public class pinyinutil { /** * 将字符串中的中文转化为拼音,其他字符不变 * * @param inputstring * @return */ public static string getpingyin(string inputstring) { hanyupinyinoutputformat format = new hanyupinyinoutputformat(); format.setcasetype(hanyupinyincasetype.lowercase); format.settonetype(hanyupinyintonetype.without_tone); format.setvchartype(hanyupinyinvchartype.with_v); char[] input = inputstring.trim().tochararray(); string output = ""; try { for (int i = 0; i < input.length; i++) { if (java.lang.character.tostring(input[i]).matches("[\\u4e00-\\u9fa5]+")) { string[] temp = pinyinhelper.tohanyupinyinstringarray(input[i], format); output += temp[0]; } else output += java.lang.character.tostring(input[i]); } } catch (badhanyupinyinoutputformatcombination e) { e.printstacktrace(); } return output; } /** * 获取汉字串拼音首字母,英文字符不变 * @param chinese 汉字串 * @return 汉语拼音首字母 */ public static string getfirstspell(string chinese) { stringbuffer pybf = new stringbuffer(); char[] arr = chinese.tochararray(); hanyupinyinoutputformat defaultformat = new hanyupinyinoutputformat(); defaultformat.setcasetype(hanyupinyincasetype.lowercase); defaultformat.settonetype(hanyupinyintonetype.without_tone); for (int i = 0; i < arr.length; i++) { if (arr[i] > 128) { try { string[] temp = pinyinhelper.tohanyupinyinstringarray(arr[i], defaultformat); if (temp != null) { pybf.append(temp[0].charat(0)); } } catch (badhanyupinyinoutputformatcombination e) { e.printstacktrace(); } } else { pybf.append(arr[i]); } } return pybf.tostring().replaceall("\\w", "").trim(); } /** * 获取汉字串拼音,英文字符不变 * @param chinese 汉字串 * @return 汉语拼音 */ public static string getfullspell(string chinese) { stringbuffer pybf = new stringbuffer(); char[] arr = chinese.tochararray(); hanyupinyinoutputformat defaultformat = new hanyupinyinoutputformat(); defaultformat.setcasetype(hanyupinyincasetype.lowercase); defaultformat.settonetype(hanyupinyintonetype.without_tone); for (int i = 0; i < arr.length; i++) { if (arr[i] > 128) { try { pybf.append(pinyinhelper.tohanyupinyinstringarray(arr[i], defaultformat)[0]); } catch (badhanyupinyinoutputformatcombination e) { e.printstacktrace(); } } else { pybf.append(arr[i]); } } return pybf.tostring(); } }
实例扩展:
import net.sourceforge.pinyin4j.pinyinhelper; public class pinyinhelperutil { /** * 得到中文首字母(中国 -> zg) * @param str 需要转化的中文字符串 * @return 大写首字母缩写的字符串 */ public static string getpinyinheadchar(string str) { stringbuilder convert = new stringbuilder(); for (int j = 0; j < str.length(); j++) { char word = str.charat(j); string[] pinyinarray = pinyinhelper.tohanyupinyinstringarray(word); if (pinyinarray != null) { convert.append(pinyinarray[0].charat(0)); } else { convert.append(word); } } return convert.tostring().touppercase(); } }
到此这篇关于java 汉字获取拼音或首字母工具类代码分析的文章就介绍到这了,更多相关java 汉字获取拼音或首字母工具类内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!