给定汉字 获得拼音首字母

汉字拼音首字母提取算法

import java.io.UnsupportedEncodingException;

public class CharacterUtil {

// 存放国标一级汉字不同读音的起始区位码
private static final int[] secPosvalueList = { 1601, 1637, 1833, 2078,2274, 2302, 2433, 2594, 2787,
3106, 3212, 3472, 3635, 3722, 3730,3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5600 };
// 存放国标一级汉字不同读音的起始区位码对应读音
private static final String[] firstLetter = { "A","B","C","D","E","F","G","H","J",
"K","L","M","N","O","P","Q","R","S","T","W","X","Y","Z","0"};

public static String getFirstLetter(String str) throws UnsupportedEncodingException{
//特殊字符:_ & @
//数字 48-57
//字母 65-90 97-122
//汉字
//空格
if(null == str){
new UnsupportedEncodingException();
}
if(str.trim().length() == 0){
return firstLetter[firstLetter.length - 1];
}
String firstString = str.trim().substring(0, 1).toUpperCase();
try {
byte[] gbCode = firstString.getBytes("GBK");
if(gbCode[0] > 0){
if(gbCode[0] >= 65 && gbCode[0] <= 90){
return firstString;
}else{
return firstLetter[firstLetter.length - 1];
}
}
int code = ((gbCode[0] & 0xff) - 160 )*100 + (gbCode[1] & 0xff) - 160;
for(int i = 0; i < secPosvalueList.length - 1; i++){
if(code >= secPosvalueList[i] && code <=secPosvalueList[i + 1]){
return firstLetter[i];
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw e;
}
return firstLetter[firstLetter.length - 1];
}

public static String[] getLetterArray(){
return firstLetter;
}

public static void main(String args[]) throws UnsupportedEncodingException{

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值