package com.framework.util;
publicclass SerialNumber {
publicstaticvoid main(String[] args) {
SerialNumber obj1 = new SerialNumber();
System.out.println(obj1.String2Alpha("陈胜"));
System.out.println(obj1.String2Alpha("吴广"));
}
//字母Z使用了两个标签,这里有27个值
//i, u, v都不做声母, 跟随前面的字母
privatechar[] chartable =
{
'啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈',
'击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然',
'撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座'
};
privatestaticchar[] alphatable =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
privatestaticint[] table = newint[27];
//初始化
{
for (int i = 0; i
table[i] = gbValue(chartable[i]);
}
}
//主函数,输入字符,得到他的声母,
//英文字母返回对应的大写字母
//其他非简体汉字返回 '0'
publicstaticchar Char2Alpha(char ch) {
if (ch >= 'a' && ch <= 'z')
return (char) (ch - 'a' + 'A');
if (ch >= 'A' && ch <= 'Z')
return ch;
int gb = gbValue(ch);
if (gb
return'0';
int i;
for (i = 0; i
if (match(i, gb)) break;
}
if (i >= 26)
return'0';
else
return alphatable[i];
}
//根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
publicstatic String String2Alpha(String SourceStr){
String Result = "";
int StrLength = SourceStr.length();
int i;
try {
for (i = 0; i
Result += Char2Alpha(SourceStr.charAt(i));
}
} catch (Exception e){
Result = "";
}
return Result;
}
privatestaticboolean match(int i, int gb){
if (gb
returnfalse;
int j = i + 1;
//字母Z使用了两个标签
while (j
if (j == 26)
return gb <= table[j];
else
return gb
}
//取出汉字的编码
privatestaticint gbValue(char ch) {
String str = new String();
str += ch;
try {
byte[] bytes = str.getBytes("GB2312");
if (bytes.length
return0;
return (bytes[0] <
0xff);
} catch (Exception e) {
return0;
}
}
}