有时候,可能会有一些类似这样的需求:

对于这样的效果,我们可以有类似这样的解决方案:

package bys.utils;

import java.io.UnsupportedEncodingException;

/**
* Created by toutou on 2014/2/21
*/
public class ChineseCharacterHelper {
static final int GB_SP_DIFF = 160;
// 存放国标一级汉字不同读音的起始区位码
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 };
// 存放国标一级汉字不同读音的起始区位码对应读音
static final char[] firstLetter = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'w', 'x', 'y', 'z' }; /**
* 提取汉字字符串的首字母
* @param characters 汉字字符串
* @return
*/
public static String getSpells(String characters) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < characters.length(); i++) { char ch = characters.charAt(i);
if ((ch >> 7) == 0) {
// 判断是否为汉字,如果左移7为为0就不是汉字,否则是汉字
buffer.append(ch);
} else {
char spell = getFirstLetter(ch);
buffer.append(String.valueOf(spell));
}
}
return buffer.toString();
} /**
* 获取一个汉字的首字母
* @param ch 汉字
* @return
*/
public static Character getFirstLetter(char ch) { byte[] uniCode = null;
try {
uniCode = String.valueOf(ch).getBytes("GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
if (uniCode[0] < 128 && uniCode[0] > 0) { // 非汉字
return null;
} else {
return convert(uniCode);
}
} /**
* 获取一个汉字的拼音首字母。 GB码两个字节分别减去160,转换成10进制码组合就可以得到区位码
* 例如汉字“你”的GB码是0xC4/0xE3,分别减去0xA0(160)就是0x24/0x43
* 0x24转成10进制就是36,0x43是67,那么它的区位码就是3667,在对照表中读音为‘n’
*/
private static char convert(byte[] bytes) {
char result = '#';
int secPosValue = 0;
int i;
for (i = 0; i < bytes.length; i++) {
bytes[i] -= GB_SP_DIFF;
}
secPosValue = bytes[0] * 100 + bytes[1];
for (i = 0; i < 23; i++) {
if (secPosValue >= secPosValueList[i] && secPosValue < secPosValueList[i + 1]) {
result = firstLetter[i];
break;
}
}
return result;
} }

调用示例  String carName = ChineseCharacterHelper.getSpells(String.valueOf(user.getName().charAt(0))).toUpperCase();

作  者:请叫我头头哥

出  处:http://www.cnblogs.com/toutou/

关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!

版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信

声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!

java 取汉字首字母的更多相关文章

  1. WPF,ComboBox,取汉字首字母,extBoxBase.TextChanged

    1取汉字汉语拼音首字母: private static string GetFirstLetterOfChineseString(string CnChar) { long iCnChar; byte ...

  2. C#取汉字首字母,汉字全拼

    使用类库为 https://gitee.com/kuiyu/dotnetcodes/tree/master/DotNet.Utilities/%E6%B1%89%E5%AD%97%E8%BD%AC%E ...

  3. sql 取汉字首字母

    )) ) --用于加密 --WITH ENCRYPTION as begin declare @intLen int ) ) set @intLen = len(@str) set @strRet = ...

  4. SQL SERVER 得到汉字首字母函数四版全集 --【叶子】

    --创建取汉字首字母函数(第三版) create function [dbo].[f_getpy_V3] ( ) ) ) as begin ),) ,@len = len(@col),@sql = ' ...

  5. php方法-------将汉字转为拼音或者提取汉字首字母

    将汉字转为全拼,提取汉字首字母 <?php /** * 基于PHP语言的汉语转拼音的类 * 兼容 UTF8.GBK.GB2312 编码,无须特殊处理 * 对中文默认返回拼音首字母缩写,其它字符不 ...

  6. java获得汉语首字母

    package org.scbit.lsbi.scp.utils; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourcefor ...

  7. MSSQL 获取汉字全拼 和 汉字首字母

    --获取全拼 DECLARE @str VARCHAR(max) SET @str= [dbo].[fn_Getquanpin]('中山') PRINT(@str) )) ) as begin ),) ...

  8. JS获取汉字首字母

    //获取 汉字首字母 function makePy(str) { if (typeof (str) != "string") throw new Error(-1, " ...

  9. java string,需要进行首字母大写改写

    java string,需要进行首字母大写改写,网上大家的思路基本一致,就是将首字母截取,转化成大写然后再串上后面的,类似如下代码 //首字母大写     public static String c ...

随机推荐

  1. Qwt 编译 配置 使用

    QWT,全称是Qt Widgets for Technical Applications,是一个基于LGPL版权协议的开源项目,可生成各种统计图.它为具有技术专业背景的程序提供GUI组件和一组实用类, ...

  2. ssh登录远程服务器

    在终端输入ssh 用户名@IP地址, 比如输入用户名和密码,进入目录,即可查看修改文件,启动服务. 这和安装xshell和filelizza,终端有什么区别? useradd  guangbo pas ...

  3. git diff old mode 100644 new mode 100755

    今天执行git diff filename ,出现 old mode 100644 new mode 100755 的提示,如下图: 但是发现文件内容并没有发生改变 想起来中间执行过chmod  的操 ...

  4. 第三方布局框架Neon初探

    github地址:https://github.com/mamaral/Neon 居中 设置 view 在 superview 的中心,调用 anchorInCenter()并设置view大小,相当于 ...

  5. Android中View的绘制流程(专题讲解)

    Android中的UI视图有两种方式实现:.xml文件(实现代码和UI的分离)和代码实现. Android的UI框架基本概念: 1. Activity:基本的页面单元,Activity包含一个Wind ...

  6. 树莓派虚拟环境手动安装HA

    树莓派手动安装 https://www.home-assistant.io/docs/installation/raspberry-pi/ sudo apt-get update sudo apt-g ...

  7. php一些高级函数方法

    PHP高级函数 1.call_user_func (http://php.net/manual/zh/function.call-user-func.php) 2.get_class (http:// ...

  8. Linux系统下,在文件中查找某个字符串

    在normal模式下按下/即可进入查找模式,输入要查找的字符串并按下回车. Vim会跳转到第一个匹配.按下n查找下一个,按下N查找上一个. Vim查找支持正则表达式,例如/vim$匹配行尾的" ...

  9. codeforces_Codeforces Round #541 (Div. 2)_abc

    A. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  10. apidoc @apiGroup兼容中文

    1.找到安装目录 中 api_group.js C:\Users\ZhiJiao361\AppData\Roaming\npm\node_modules\apidoc\node_modules\api ...