ed8ae40e221bf061d341fce10e334138253c9828.svn-base 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package cn.com.goldenwater.dcproj.utils;
  2. import net.sourceforge.pinyin4j.PinyinHelper;
  3. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  7. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  8. public class PinYin {
  9. //返回一个拼音字符串,并且首字母大写
  10. public String getPingYin(String src) {
  11. char[] t1 = null;
  12. t1 = src.toCharArray();
  13. String[] t2 = new String[t1.length];
  14. HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
  15. t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  16. t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  17. t3.setVCharType(HanyuPinyinVCharType.WITH_V);
  18. String t4 = "";
  19. int t0 = t1.length;
  20. try {
  21. for (int i = 0; i < t0; i++) {
  22. // 判断是否为汉字字符
  23. if (Character.toString(t1[i]).matches(
  24. "[\\u4E00-\\u9FA5]+")) {
  25. t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
  26. t4 += t2[0].substring(0, 1).toUpperCase() + t2[0].substring(1) + " ";
  27. } else {
  28. t4 += Character.toString(t1[i]) + " ";
  29. }
  30. }
  31. } catch (BadHanyuPinyinOutputFormatCombination e1) {
  32. e1.printStackTrace();
  33. }
  34. return t4;
  35. }
  36. //返回中文的大写首字母
  37. public String getPinYinHeadChar(String str) {
  38. String convert = "";
  39. for (int j = 0; j < str.length(); j++) {
  40. char word = str.charAt(j);
  41. String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
  42. if (pinyinArray != null) {
  43. convert += pinyinArray[0].toUpperCase().charAt(0);
  44. } else {
  45. convert += word;
  46. }
  47. }
  48. return convert;
  49. }
  50. }