隨筆-5  評論-4  文章-0  trackbacks-0


          以下是源代碼:



          import net.sourceforge.pinyin4j.PinyinHelper;
          import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
          import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
          import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
          import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

          public class SpellHelper {
              //將中文轉換為英文
              public static String getEname(String name) {
                  HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
                  pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
                  pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
                  pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

                  return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
              }

              //姓、名的第一個字母需要為大寫
              public static String getUpEname(String name) {
                  char[] strs = name.toCharArray();
                  String newname = null;
                          
                  //名字的長度
                  if (strs.length == 2) {    
                          newname = toUpCase(getEname("" + strs[0])) + " "
                              + toUpCase(getEname("" + strs[1]));
                  } else if (strs.length == 3) {
                         newname = toUpCase(getEname("" + strs[0])) + " "
                              + toUpCase(getEname("" + strs[1] + strs[2]));
                  } else if (strs.length == 4) {
                      newname = toUpCase(getEname("" + strs[0] + strs[1])) + " "
                              + toUpCase(getEname("" + strs[2] + strs[3]));
                  } else {
                      newname = toUpCase(getEname(name));
                  }

                  return newname;
              }

              //首字母大寫
              private static String toUpCase(String str) {
                  StringBuffer newstr = new StringBuffer();
                  newstr.append((str.substring(0, 1)).toUpperCase()).append(
                          str.substring(1, str.length()));

                  return newstr.toString();
              }

              public static void main(String[] args) {
                  System.out.println(getUpEname("李宇春"));

              }

          }

           

          大家如果覺得有用可以關注社區地址http://www.javadt.com

          posted on 2012-04-25 11:25 低··調 閱讀(2747) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 吴旗县| 光泽县| 寻甸| 清远市| 桃江县| 商丘市| 金华市| 庆城县| 肃南| 临江市| 瑞金市| 郸城县| 建水县| 蕉岭县| 舞阳县| 张家川| 思茅市| 安岳县| 内黄县| 兰西县| 申扎县| 保靖县| 永泰县| 永善县| 芜湖市| 秀山| 安阳市| 台东市| 鱼台县| 许昌县| 林周县| 锡林浩特市| 平果县| 静海县| 连江县| 无锡市| 盱眙县| 资溪县| 茶陵县| 隆回县| 陆河县|