Java拼音拆分算法--Java学习网

时间:2016-12-8 8:02:50

  核心提示:public static String polishPinyin(String name) {if (name == null || ''.equals(name))return name;name...
  1. public static String polishPinyin(String name) {
  2. if (name == null || "".equals(name))
  3. return name;
  4. name = name.replaceAll("[0-9_]+", "");
  5. StringBuffer buffer = new StringBuffer();
  6. char[] chars = name.toCharArray();
  7. int size = chars.length;
  8. int i = 0;
  9. boolean flag = true;// 当前检测的是声母还是韵母
  10. while (i < size) {
  11. if (flag) {
  12. // 先判断前两位是不是声母
  13. if (i + 1 < size && PinyinUtil.isShengmu(chars[i], chars[i + 1])) {
  14. buffer.append(chars, i, 2);
  15. i += 2;
  16. flag = false;
  17. } else if (PinyinUtil.isShengmu(chars[i])) {
  18. buffer.append(chars[i]);
  19. flag = false;
  20. i++;
  21. } else {
  22. i++;
  23. }
  24. if (i == size)
  25. buffer.append("%");
  26. } else {
  27. if (i + 2 < size && PinyinUtil.isYunmu(chars[i], chars[i + 1], chars[i + 2])) {
  28. buffer.append(chars, i, 3);
  29. i += 3;
  30. } else if (i + 1 < size && PinyinUtil.isYunmu(chars[i], chars[i + 1])) {
  31. buffer.append(chars, i, 2);
  32. i += 2;
  33. } else if (PinyinUtil.isYunmu(chars[i])) {
  34. buffer.append(chars[i]);
  35. i++;
  36. } else {
  37. buffer.append("%");
  38. }
  39. flag = true;
  40. }
  41. }
  42. return buffer.toString();
  43. }
复制代码
  1. public class PinyinUtil {
  2. private static final String[] Shengmu = { "b", "p", "m", "f", "d", "t", "l", "n", "g", "k", "h", "j", "q", "x", "z",
  3. "c", "s", "zh", "ch", "sh", "y", "w" };
  4. private static final String[] Yunmu = { "a", "o", "e", "i", "u", "v", "ai", "ei", "ui", "ao", "ou", "iu", "ie",
  5. "ue", "ve", "er", "an", "en", "in", "un", "ang", "eng", "ing", "ong" };
  6. public static boolean isShengmu(String str) {
  7. for (String shengmu : Shengmu)
  8. if (shengmu.equalsIgnoreCase(str))
  9. return true;
  10. return false;
  11. }
  12. public static boolean isShengmu(char c) {
  13. return isShengmu(String.valueOf(c));
  14. }
  15. public static boolean isShengmu(char c1, char c2) {
  16. char[] chars = { c1, c2 };
  17. return isShengmu(String.valueOf(chars));
  18. }
  19. public static boolean isYunmu(String str) {
  20. for (String yunmu : Yunmu)
  21. if (yunmu.equalsIgnoreCase(str))
  22. return true;
  23. return false;
  24. }
  25. public static boolean isYunmu(char c) {
  26. return isYunmu(String.valueOf(c));
  27. }
  28. public static boolean isYunmu(char c1, char c2) {
  29. char[] chars = { c1, c2 };
  30. return isYunmu(String.valueOf(chars));
  31. }
  32. public static boolean isYunmu(char c1, char c2, char c3) {
  33. char[] chars = { c1, c2, c3 };
  34. return isYunmu(String.valueOf(chars));
  35. }
  36. }

Java免费学习   Java自学网 http://www.javalearns.com

关注微信号:javalearns   随时随地学Java

或扫一扫

随时随地学Java

作者:不详 来源:网络
    你是从哪里知道本网站的?
  • 网友介绍的
  • 百度搜索的
  • Google搜索的
  • 其它搜索过来的
  • 网址输错了进来的
  • 太忙了不记得了
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • java学习网(www.javalearns.com) © 2014 版权所有 All Rights Reserved.
  • Email:javalearns@163.com 站长QQ:1356121699 晋ICP备14003680号-3
  • java学习网部分内容来自网络或网友发布,如侵犯了您利益,请发邮件至:javalearns@126.com,我们尽快处理!
  • Java学习网
  • 网站统计
  • 晋公网安备 14042902000001号