java之母_java 得到中文首字母

import java.util.Random;

/***

*

* 得到中文首字母

*

* @author lxm_09

*

*/

public class ChinaInitial {

/**

*

* @param args

*

*/

public static void main(String[] args) {

String str = "我是中国人";

System.out.println("中文首字母:" + getPYIndexStr(str, true));

}

/**

*

* 返回首字母

*

* @param strChinese

*

* @param bUpCase

*

* @return

*

*/

public static String getPYIndexStr(String strChinese, boolean bUpCase) {

try {

StringBuffer buffer = new StringBuffer();

byte b[] = strChinese.getBytes("GBK");// 把中文转化成byte数组

for (int i = 0; i < b.length; i++) {

if ((b[i] & 255) > 128) {

int char1 = b[i++] & 255;

char1 <<= 8;// 左移运算符用“<

int chart = char1 + (b[i] & 255);

buffer.append(getPYIndexChar((char) chart, bUpCase));

continue;

}

char c = (char) b[i];

// 确定指定字符是否可以是 Java 标识符中首字符以外的部分。

if (!Character.isJavaIdentifierPart(c)) {

c = 'A';

}

buffer.append(c);

}

return buffer.toString();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

*

* 得到首字母

*

* @param strChinese

*

* @param bUpCase

*

* @return

*

*/

private static char getPYIndexChar(char strChinese, boolean bUpCase) {

int charGBK = strChinese;

char result;

if (charGBK >= 45217 && charGBK <= 45252)

result = 'A';

else if (charGBK >= 45253 && charGBK <= 45760)

result = 'B';

else if (charGBK >= 45761 && charGBK <= 46317)

result = 'C';

else if (charGBK >= 46318 && charGBK <= 46825)

result = 'D';

else if (charGBK >= 46826 && charGBK <= 47009)

result = 'E';

else if (charGBK >= 47010 && charGBK <= 47296)

result = 'F';

else if (charGBK >= 47297 && charGBK <= 47613)

result = 'G';

else if (charGBK >= 47614 && charGBK <= 48118)

result = 'H';

else if (charGBK >= 48119 && charGBK <= 49061)

result = 'J';

else if (charGBK >= 49062 && charGBK <= 49323)

result = 'K';

else if (charGBK >= 49324 && charGBK <= 49895)

result = 'L';

else if (charGBK >= 49896 && charGBK <= 50370)

result = 'M';

else if (charGBK >= 50371 && charGBK <= 50613)

result = 'N';

else if (charGBK >= 50614 && charGBK <= 50621)

result = 'O';

else if (charGBK >= 50622 && charGBK <= 50905)

result = 'P';

else if (charGBK >= 50906 && charGBK <= 51386)

result = 'Q';

else if (charGBK >= 51387 && charGBK <= 51445)

result = 'R';

else if (charGBK >= 51446 && charGBK <= 52217)

result = 'S';

else if (charGBK >= 52218 && charGBK <= 52697)

result = 'T';

else if (charGBK >= 52698 && charGBK <= 52979)

result = 'W';

else if (charGBK >= 52980 && charGBK <= 53688)

result = 'X';

else if (charGBK >= 53689 && charGBK <= 54480)

result = 'Y';

else if (charGBK >= 54481 && charGBK <= 55289)

result = 'Z';

else

result = (char) (65 + (new Random()).nextInt(25));

if (!bUpCase)

result = Character.toLowerCase(result);

return result;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值