java取得汉语首字母代码

//调用方式 ChinesFirstCode.GetCharSpellCode(unit.getDwmc();
public class ChinesFirstCode {

	/**
	 * 汉字首字母生成工具
	 */
	private final static int finalA = 45217;
    private final static int finalB = 45253;
    private final static int finalC = 45761;
    private final static int finalD = 46318;
    private final static int finalE = 46826;
    private final static int finalF = 47010;
    private final static int finalG = 47297;
    private final static int finalH = 47614;
    private final static int finalJ = 48119;
    private final static int finalK = 49062;
    private final static int finalL = 49324;
    private final static int finalM = 49896;
    private final static int finalN = 50371;
    private final static int finalO = 50614;
    private final static int finalP = 50622;
    private final static int finalQ = 50906;
    private final static int finalR = 51387;
    private final static int finalS = 51446;
    private final static int finalT = 52218;
    private final static int finalW = 52698;
    private final static int finalX = 52980;
    private final static int finalY = 53689;
    private final static int finalZ = 54481;

    public ChinesFirstCode()
    { }
    
   //调用GetCharSpellCode(String sourceStr)即可
    public static String GetCharSpellCode(String sourceStr) throws Exception  
    {
        long iCnChar=0;
        char tempChar;
        byte[] HZ=new byte[2];
        StringBuilder builderStr=new StringBuilder();
        String result="";
        for(int i=0;i<sourceStr.length();i++)
        {
        	tempChar=sourceStr.charAt(i);
        	String tempStr=String.valueOf(tempChar);
        	HZ = tempStr.getBytes("gb2312");
            if (HZ.length == 1)
            {
            	builderStr.append(sourceStr.toUpperCase()) ;
            }
            else
            {
                int a1 = (short)(HZ[0])+256;
                int a2 = (short)(HZ[1])+256;
                iCnChar=a1 * 256 + a2;
                builderStr.append(GetChar(iCnChar));
            }
            
        }
        result=builderStr.toString();
        return result;
    }

    private static String GetChar(long iCnChar)
    {
        if (CompareChar(iCnChar, finalA, finalB))
        {
            return "A";
        }
        if (CompareChar(iCnChar, finalB, finalC))
        {
            return "B";
        }
        if (CompareChar(iCnChar, finalC, finalD))
        {
            return "C";
        }
        if (CompareChar(iCnChar, finalD, finalE))
        {
            return "D";
        }
        if (CompareChar(iCnChar, finalE, finalF))
        {
            return "E";
        }
        if (CompareChar(iCnChar, finalF, finalG))
        {
            return "F";
        }
        if (CompareChar(iCnChar, finalG, finalH))
        {
            return "G";
        }
        if (CompareChar(iCnChar, finalH, finalJ))
        {
            return "H";
        }
        if (CompareChar(iCnChar, finalJ, finalK))
        {
            return "J";
        }
        if (CompareChar(iCnChar, finalK, finalL))
        {
            return "K";
        }
        if (CompareChar(iCnChar, finalL, finalM))
        {
            return "L";
        }
        if (CompareChar(iCnChar, finalM, finalN))
        {
            return "M";
        }
        if (CompareChar(iCnChar, finalN, finalO))
        {
            return "N";
        }
        if (CompareChar(iCnChar, finalO, finalP))
        {
            return "O";
        }
        if (CompareChar(iCnChar, finalP, finalQ))
        {
            return "P";
        }
        if (CompareChar(iCnChar, finalQ, finalR))
        {
            return "Q";
        }
        if (CompareChar(iCnChar, finalR, finalS))
        {
            return "R";
        }
        if (CompareChar(iCnChar, finalS, finalT))
        {
            return "S";
        }
        if (CompareChar(iCnChar, finalT, finalW))
        {
            return "T";
        }
        if (CompareChar(iCnChar, finalW, finalX))
        {
            return "W";
        }
        if (CompareChar(iCnChar, finalX, finalY))
        {
            return "X";
        }
        if (CompareChar(iCnChar, finalY, finalZ))
        {
            return "Y";
        }
        if (CompareChar(iCnChar, finalZ, 55289))
        {
            return "Z";
        }
        else
        {
            return "*";
        }
    }
    private static boolean CompareChar(long iCnChar, int S, int E)
    {
        if (iCnChar >= S && iCnChar < E)
        {
            return true;
        }
        else
        {
            return false;
        }

    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值