[Java学习] Java 读取汉字全拼简写

北风网AD
需要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.kmnk01.com/hxpfk/2015/bdf_1219/312.html
  8. public class ChineseToSpell {
  9. /**
  10. * 得到 全拼
  11. *
  12. * @param src
  13. * @return
  14. */
  15. public static String getFullSpell(String src) {
  16. char[] t1 = null;
  17. t1 = src.toCharArray();
  18. String[] t2 = new String[t1.length];
  19. HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
  20. t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  21. t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  22. t3.setVCharType(HanyuPinyinVCharType.WITH_V);
  23. String t4 = "";http://www.kmnk01.com/hxpfk/2015/xmz_1219/320.html
  24. int t0 = t1.length;
  25. try {
  26. for (int i = 0; i < t0; i++) {
  27. // 判断是否为汉字字符
  28. if (java.lang.Character.toString(t1[i]).matches("[\u4e00-\u9fa5]+")) {
  29. t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
  30. t4 += t2[0];
  31. } else {
  32. http://www.kmnk01.com/hxpfk/2015/gm_1219/314.html
  33. t4 += java.lang.Character.toString(t1[i]);
  34. }
  35. }
  36. return t4;
  37. } catch (BadHanyuPinyinOutputFormatCombination e1) {
  38. http://www.kmnk01.com/hxpfk/2015/ylb_1219/319.html
  39. e1.printStackTrace();
  40. }
  41. return t4;
  42. }
  43. /**
  44. * 得到中文首字母
  45. *http://www.kmnk01.com/hxpfk/2015/mny_1219/315.html
  46. * @param str
  47. * @return
  48. */
  49. public static String getPinYinHeadChar(String str) {
  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.kmnk01.com/hxpfk/2015/qcd_1219/318.html
  59. }
  60. return convert;
  61. }
  62. /**
  63. * 将字符串转移为ASCII码
  64. *
  65. * @param cnStr
  66. * @return
  67. */http://www.kmnk01.com/hxpfk/2015/py_1219/321.html
  68. public static String getCnASCII(String cnStr) {
  69. StringBuffer strBuf = new StringBuffer();
  70. byte[] bGBK = cnStr.getBytes();
  71. for (int i = 0; i < bGBK.length; i++) {
  72. // System.out.println(Integer.toHexString(bGBK[i]&0xff));
  73. strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
  74. }http://www.kmnk01.com/hxpfk/2015/sz_1219/313.html
  75. return strBuf.toString();

  76. }
  77. // public static void main(String[] args) {
  78. // long currentTime=System.currentTimeMillis();
  79. // String cnStr = "丁叉基酜内酯";
  80. // System.out.println(getPingYin(cnStr));
  81. // System.out.println(getPinYinHeadChar(cnStr));
  82. // System.out.println(System.currentTimeMillis()-currentTime);
  83. http://www.kmnk01.com/hxpfk/2015/py_1219/316.html
  84. // }
  85. public static String getPinYinHeadChar(String str) {
    1. String convert = "";
    2. for (int j = 0; j < str.length(); j++) {
    3. char word = str.charAt(j);
    4. String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
    5. if (pinyinArray != null) {
    6. convert += pinyinArray[0].charAt(0);
    7. } else {
    8. convert += word;
    9. http://www.kmnk01.com/hxpfk/2015/sz_1219/317.html
    10. }
    11. return convert;
    12. }
    13. /**
    14. * 将字符串转移为ASCII码

  86. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值