zoukankan      html  css  js  c++  java
  • Java将中文姓名转换为拼音

    代码如下:

     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 public class SpellHelper {
     7     //将中文转换为英文
     8     public static String getEname(String name)
     9      {
    10         HanyuPinyinOutputFormat pyFormat = new        HanyuPinyinOutputFormat();
    11          pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    12         pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    13         pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
    14 
    15         return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
    16     }
    17 
    18     //姓、名的第一个字母需要为大写
    19     public static String getUpEname(String name) {
    20         char[] strs = name.toCharArray();
    21         String newname = null;
    22                 
    23         //名字的长度
    24         if (strs.length == 2) {    
    25                 newname = toUpCase(getEname("" + strs[0])) + " "
    26                     + toUpCase(getEname("" + strs[1]));
    27         } else if (strs.length == 3) 
    28          {
    29                newname = toUpCase(getEname("" + strs[0])) + " "
    30                     + toUpCase(getEname("" + strs[1] + strs[2]));
    31         } 
    32        else if (strs.length == 4) 
    33        {
    34             newname = toUpCase(getEname("" + strs[0] + strs[1])) + " "
    35                     + toUpCase(getEname("" + strs[2] + strs[3]));
    36         } else 
    37         {
    38             newname = toUpCase(getEname(name));
    39         }
    40         return newname;
    41     }
    42 
    43     //首字母大写
    44     private static String toUpCase(String str) {
    45         StringBuffer newstr = new StringBuffer();
    46         newstr.append((str.substring(0, 1)).toUpperCase()).append(
    47                 str.substring(1, str.length()));
    48 
    49         return newstr.toString();
    50     }
    51     public static void main(String[] args) {
    52         System.out.println(getUpEname("李宇春"));
    53 
    54     }
    55 
    56 }
  • 相关阅读:
    Spark&Hadoop:scala编写spark任务jar包,运行无法识别main函数,怎么办?
    Linux:krb5
    SqlServer数据库端口默认是1433吗?
    Linux下使用shell实现上传linux下某个目录下所有文件到ftp
    Spark+Hadoop+Hive集群上数据操作记录
    hadoop之 Hadoop2.2.0中HDFS的高可用性实现原理
    虚拟路由冗余协议VRRP
    hadoop 之Hadoop生态系统
    Oracle NoLogging Append 方式减少批量insert的redo_size
    Oracle常用的性能诊断语句
  • 原文地址:https://www.cnblogs.com/ayan/p/2451128.html
Copyright © 2011-2022 走看看