//调用方式 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;
}
}
}
java取得汉语首字母代码
最新推荐文章于 2021-08-20 16:20:55 发布