PinyinUtils.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.hcks.cmfds.commons.util;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import net.sourceforge.pinyin4j.PinyinHelper;
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  7. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  8. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  9. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  10. /**
  11. *
  12. *
  13. * @Author 刘厦 (liusha.information@gmail.com)
  14. * @Date 创建时间:2011-8-4,下午02:18:23
  15. * @Version 0.0.0
  16. *
  17. * 类说明:
  18. *
  19. */
  20. public class PinyinUtils {
  21. public static int HANZI_UNICODE_START = 0x4e00;
  22. public static int HANZI_UNICODE_END = 0x9fa5;
  23. /**
  24. * 取得字符串拼音的首字母 (汉字之外字符不转化)
  25. * @param name
  26. * @return
  27. */
  28. public static String getPinyinInitial(String name) {
  29. List<Character> cl = new ArrayList<Character>(name.length());
  30. HanyuPinyinOutputFormat f = new HanyuPinyinOutputFormat();
  31. f.setCaseType(HanyuPinyinCaseType.UPPERCASE);
  32. f.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  33. f.setVCharType(HanyuPinyinVCharType.WITH_V);
  34. try {
  35. for (int i = 0; i < name.length(); i++) {
  36. char c = name.charAt(i);
  37. if (c >= HANZI_UNICODE_START && c < HANZI_UNICODE_END) {
  38. String[] py = PinyinHelper.toHanyuPinyinStringArray(name.charAt(i), f);
  39. if (py != null) {
  40. cl.add(py[0].charAt(0));
  41. }
  42. } else {
  43. cl.add(Character.toUpperCase(c));
  44. }
  45. }
  46. } catch (BadHanyuPinyinOutputFormatCombination e) {
  47. }
  48. char[] ca = new char[cl.size()];
  49. for (int i = 0; i < ca.length; i++) {
  50. ca[i] = cl.get(i);
  51. }
  52. return new String(ca);
  53. }
  54. /**
  55. * 取 汉字拼音全拼 (多音字只取最常用读音)
  56. * @param name
  57. * @return
  58. */
  59. public static String getPinyinComplete(String name) {
  60. StringBuilder result = new StringBuilder();
  61. HanyuPinyinOutputFormat f = new HanyuPinyinOutputFormat();
  62. f.setCaseType(HanyuPinyinCaseType.UPPERCASE);
  63. f.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  64. f.setVCharType(HanyuPinyinVCharType.WITH_V);
  65. try {
  66. for (int i = 0; i < name.length(); i++) {
  67. char c = name.charAt(i);
  68. if (c >= HANZI_UNICODE_START && c < HANZI_UNICODE_END) {
  69. String[] py = PinyinHelper.toHanyuPinyinStringArray(name.charAt(i), f);
  70. if (py != null) {
  71. result.append(py[0]);
  72. }
  73. } else {
  74. result.append(c);
  75. }
  76. }
  77. } catch (BadHanyuPinyinOutputFormatCombination e) {
  78. }
  79. return result.toString();
  80. }
  81. public static void main(String[] args) {
  82. System.out.println(PinyinUtils.getPinyinInitial("虚空假面"));
  83. System.out.println(PinyinUtils.getPinyinInitial("虚空假面111"));
  84. System.out.println(PinyinUtils.getPinyinInitial("虚空假面11aa"));
  85. }
  86. }