Java 读取汉字全拼简写

需要pinyin4j这个类库: http://pinyin4j.sourceforge.net/
  1. import net.sourceforge.pinyin4j.PinyinHelper;
  2. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  3. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  6. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  7. http://www.kmnk03.com/hxpfk/dzpz/334.html
  8. public class ChineseToSpell {
  9. http://www.kmnk03.com/hxpfk/npx/343.html
  10. /**
  11. * 得到 全拼
  12. *
  13. * @param src
  14. * @return
  15. */http://www.kmnk03.com/hxpfk/tf/335.html
  16. public static String getFullSpell(String src) {
  17. char[] t1 = null;
  18. t1 = src.toCharArray();
  19. String[] t2 = new String[t1.length];
  20. HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
  21. t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  22. t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  23. t3.setVCharType(HanyuPinyinVCharType.WITH_V);
  24. String t4 = "";
  25. int t0 = t1.length;
  26. try {
  27. for (int i = 0; i < t0; i++) {
  28. // 判断是否为汉字字符
  29. if (java.lang.Character.toString(t1[i]).matches("[\u4e00-\u9fa5]+")) {
  30. t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
  31. t4 += t2[0];
  32. } else {
  33. t4 += java.lang.Character.toString(t1[i]);
  34. }
  35. }http://www.kmnk03.com/hxpfk/py/336.html
  36. return t4;
  37. } catch (BadHanyuPinyinOutputFormatCombination e1) {
  38. e1.printStackTrace();
  39. }
  40. return t4;
  41. }http://www.kmnk03.com/hxpfk/npx/342.html
  42. /**
  43. * 得到中文首字母
  44. *
  45. * @param str
  46. * @return
  47. */
  48. public static String getPinYinHeadChar(String str) {
  49. http://www.kmnk03.com/hxpfk/npx/337.html
  50. String convert = "";
  51. for (int j = 0; j < str.length(); j++) {
  52. char word = str.charAt(j);
  53. String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
  54. if (pinyinArray != null) {
  55. convert += pinyinArray[0].charAt(0);
  56. } else {
  57. convert += word;
  58. }http://www.kmnk03.com/hxpfk/npx/341.html
  59. }
  60. return convert;
  61. }
  62. http://www.kmnk03.com/hxpfk/npx/338.html
  63. /**
  64. * 将字符串转移为ASCII码
  65. *
  66. * @param cnStr
  67. * @return
  68. */
  69. public static String getCnASCII(String cnStr) {
  70. StringBuffer strBuf = new StringBuffer();
  71. byte[] bGBK = cnStr.getBytes();
  72. for (int i = 0; i < bGBK.length; i++) {
  73. // System.out.println(Integer.toHexString(bGBK[i]&0xff));
  74. strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
  75. }
  76. return strBuf.toString();
  77. }http://www.kmnk03.com/hxpfk/py/339.html
  78. http://www.kmnk03.com/hxpfk/npx/340.html
  79. // public static void main(String[] args) {
  80. // long currentTime=System.currentTimeMillis();
  81. // String cnStr = "丁叉基酜内酯";
  82. // System.out.println(getPingYin(cnStr));
  83. // System.out.println(getPinYinHeadChar(cnStr));
  84. // System.out.println(System.currentTimeMillis()-currentTime);
  85. // }kmnk03.com
  86. www,kmnk03.com
  87. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值