• 汉字转拼音首字母的java实现


    工作中经常会遇到的一些排序问题,比如 按汉字的拼音首字母排序,比如人名排序等,就要用到下面的方法了,思路:

    1. 获得汉字

    2. 将汉字转换成首字母,并记录下(必要时保存到数据库)

    3. 按首字母进行排序并展示

    控制台输出演示:

    汉字转首字母演示,请输入汉字:           4654^*&^&*^_FDSF你i好3啊>?>?>*(*33P{}{
    
    直接转换convertTo:                   4654^*&^&*^_FDSFnih3a>?>?>*(*33P{}{
    保留一些字符convertAndClear:         4654^&^&^_FDSFnih3a>?>?>(33P{}{
    仅保留字母和数字convertAndClearAll:   4654FDSFnih3a33P

    代码如下,直接复制后就能使用:

    package pinyin;
    
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("汉字转首字母演示,请输入汉字:");
            String str = sc.next();
            String py1 = ChineseToFirstCharUtil.convertTo(str);
            String py2 = ChineseToFirstCharUtil.convertAndClear(str);
            String py3 = ChineseToFirstCharUtil.convertAndClearAll(str);
            System.out.print("
    直接转换convertTo:" + py1);
            System.out.print("
    保留一些字符convertAndClear:" + py2);
            System.out.print("
    仅保留字母和数字convertAndClearAll:" + py3);
        }
    }
    package pinyin;
    
    /**
     * 汉字转首字母缩写
     * 
     * @author 张云飞vir基于网上获得的资料的修改2015-3-3 http://home.cnblogs.com/u/vir56k/
     *
     */
    public class ChineseToFirstCharUtil {
    /** * 汉字转拼音缩写 * * @param str * 要转换的汉字字符串 * @return String 拼音缩写 */ public static String convertTo(String str) { String tempStr = ""; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c >= 33 && c <= 126) {// 字母和符号原样保留 tempStr += String.valueOf(c); } else {// 累加拼音声母 tempStr += getPYChar(String.valueOf(c)); } } return tempStr; } /** * 汉字转拼音缩写,清理无效字符(保留一些键盘字符) * @param str 汉字 * @return String 缩写 */ public static String convertAndClear(String str) { return convertTo(str).replace("*", ""); } /** * 汉字转拼音缩写,清理无效字符(清理任何非数字和字母) * @param str 汉字 * @return String 缩写 */ public static String convertAndClearAll(String str) { String str1 = convertTo(str).replace("*", ""); StringBuilder sb = new StringBuilder(); for(char c : str1.toCharArray()){ if((c>=48 && c<= 57) || (c>=65 && c<= 90) || (c>=97 && c<= 122)){ sb.append(c); } } return sb.toString(); } /** * 取单个字符的拼音声母 * * @param c * //要转换的单个汉字 * @return String 拼音声母 */ private static String getPYChar(String c) { byte[] array = new byte[2]; array = String.valueOf(c).getBytes(); int i = (short) (array[0] - '' + 256) * 256 + ((short) (array[1] - '' + 256)); if (i < 0xB0A1) return "*"; if (i < 0xB0C5) return "a"; if (i < 0xB2C1) return "b"; if (i < 0xB4EE) return "c"; if (i < 0xB6EA) return "d"; if (i < 0xB7A2) return "e"; if (i < 0xB8C1) return "f"; if (i < 0xB9FE) return "g"; if (i < 0xBBF7) return "h"; if (i < 0xBFA6) return "j"; if (i < 0xC0AC) return "k"; if (i < 0xC2E8) return "l"; if (i < 0xC4C3) return "m"; if (i < 0xC5B6) return "n"; if (i < 0xC5BE) return "o"; if (i < 0xC6DA) return "p"; if (i < 0xC8BB) return "q"; if (i < 0xC8F6) return "r"; if (i < 0xCBFA) return "s"; if (i < 0xCDDA) return "t"; if (i < 0xCEF4) return "w"; if (i < 0xD1B9) return "x"; if (i < 0xD4D1) return "y"; if (i < 0xD7FA) return "z"; return "*"; } }
  • 相关阅读:
    Airflow使用笔记
    公共关系学(第二版)笔记
    公众关系秘籍
    SQL SERVER XML 学习总结
    hadoop 1.2.1 配置
    ssh
    centos&windows的共享文件(通过挂载)
    代理设置(wget/yum)
    环境变量设置
    centos7 自定义服务
  • 原文地址:https://www.cnblogs.com/vir56k/p/4310239.html
Copyright © 2020-2023  润新知