代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
获取汉字首字母
作者:
涌
/ 发布于
2016/12/21
/
729
获取汉字首字母
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; public class Chinesetoen { private final int[] li_SecPosValue = { 1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590 }; private final String[] lc_FirstLetter = { "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "w", "x", "y", "z" }; private final String ls_SecondSecTable = "CJWGNSPGCGNE[Y[BTYYZDXYKYGT[JNNJQMBSGZSCYJSYY[PGKBZGY[YWJKGKLJYWKPJQHY[W[DZLSGMRYPYWWCCKZNKYYGTTNJJNYKKZYTCJNMCYLQLYPYQFQRPZSLWBTGKJFYXJWZLTBNCXJJJJTXDTTSQZYCDXXHGCK[PHFFSS[YBGXLPPBYLL[HLXS[ZM[JHSOJNGHDZQYKLGJHSGQZHXQGKEZZWYSCSCJXYEYXADZPMDSSMZJZQJYZC[J[WQJBYZPXGZNZCPWHKXHQKMWFBPBYDTJZZKQHYLYGXFPTYJYYZPSZLFCHMQSHGMXXSXJ[[DCSBBQBEFSJYHXWGZKPYLQBGLDLCCTNMAYDDKSSNGYCSGXLYZAYBNPTSDKDYLHGYMYLCXPY[JNDQJWXQXFYYFJLEJPZRXCCQWQQSBNKYMGPLBMJRQCFLNYMYQMSQYRBCJTHZTQFRXQHXMJJCJLXQ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QAMMMYXCTPSHCPTXXZZSMPHPSHMCLMLDQFYQXSZYYDYJZZHQPDSZGLSTJBCKBXYQZJSGPSXQZQZRQTBDKYXZKHHGFLBCSMDLDGDZDBLZYYCXNNCSYBZBFGLZZXSWMSCCMQNJQSBDQSJTXXMBLTXZCLZSHZCXRQJGJYLXZFJPHYMZQQYDFQJJLZZNZJCDGZYGCTXMZYSCTLKPHTXHTLBJXJLXSCDQXCBBTJFQZFSLTJBTKQBXXJJLJCHCZDBZJDCZJDCPRNPQCJPFCZLCLZXZDMXMPHJSGZGSZZQLYLWTJPFSYASMCJBTZKYCWMYTCSJJLJCQLWZMALBXYFBPNLSFHTGJWEJJXXGLLJSTGSHJQLZFKCGNNNSZFDEQFHBSAQTGYLBXMMYGSZLDYDQMJJRGBJTKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ "; int li_offset; /** * \u53D6\u5F97\u7ED9\u5B9A\u6C49\u5B57\u4E32\u7684\u9996\u5B57\u6BCD\u4E32,\u5373\u58F0\u6BCD\u4E32 * * @param str * \u7ED9\u5B9A\u6C49\u5B57\u4E32 * @return \u58F0\u6BCD\u4E32 */ public String getAllFirstLetter(String str) { if (str == null || str.trim().length() == 0) { return ""; } String _str = ""; for (int i = 0; i < str.length(); i++) { _str = _str + this.getFirstLetter(str.substring(i, i + 1)); } return _str; } /** * \u53D6\u5F97\u7ED9\u5B9A\u6C49\u5B57\u7684\u9996\u5B57\u6BCD,\u5373\u58F0\u6BCD * * @param chinese * \u7ED9\u5B9A\u7684\u6C49\u5B57 * @return \u7ED9\u5B9A\u6C49\u5B57\u7684\u58F0\u6BCD */ //\u8F93\u5165\u5B57\u7B26\u4E32\u6C49\u5B57\uFF0C\u8FD4\u56DE\u5B57\u7B26\u4E32\uFF0C\u5982\u679C\u6709\u6C49\u5B57\uFF0C\u5C06\u6C49\u5B57\u7684\u9996\u5B57\u6BCD\u7684\u8FD4\u56DE private String getFirstLetter(String chinese) { if (chinese == null || chinese.trim().length() == 0) { return ""; } chinese = conversionStr(chinese, "GB2312", "ISO8859-1"); if (chinese.length() > 1) // \u5224\u65AD\u662F\u4E0D\u662F\u6C49\u5B57 { int li_SectorCode = (int) chinese.charAt(0); // \u6C49\u5B57\u533A\u7801 int li_PositionCode = (int) chinese.charAt(1); // \u6C49\u5B57\u4F4D\u7801 li_SectorCode = li_SectorCode - 160; li_PositionCode = li_PositionCode - 160; int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // \u6C49\u5B57\u533A\u4F4D\u7801 if (li_SecPosCode > 1600 && li_SecPosCode < 5590) { for (int i = 0; i < 23; i++) { if (li_SecPosCode >= li_SecPosValue[i] && li_SecPosCode < li_SecPosValue[i + 1]) { chinese = lc_FirstLetter[i].toUpperCase(); break; } } } else { // \u7B2C\u4E00\u4E2A\u5B57\u7B26 li_offset = (li_SectorCode - 56) * 94 + li_PositionCode - 1; // \u8BA1\u7B97\u504F\u79FB\u91CF if (li_offset >= 0 && li_offset <= 3007) { // \u4E8C\u533A\u6C49\u5B57 chinese = ls_SecondSecTable.substring(li_offset, li_offset + 1); // \u53D6\u51FA\u6B64\u5B57\u58F0\u6BCD } else // \u975E\u6C49\u5B57\u5B57\u7B26,\u5982\u56FE\u5F62\u7B26\u53F7\u6216ASCII\u7801 { chinese = conversionStr(chinese, "ISO8859-1", "GB2312"); chinese = chinese.substring(0, 1); } } } return chinese; } /** * \u5B57\u7B26\u4E32\u7F16\u7801\u8F6C\u6362 * * @param str * \u8981\u8F6C\u6362\u7F16\u7801\u7684\u5B57\u7B26\u4E32 * @param charsetName * \u539F\u6765\u7684\u7F16\u7801 * @param toCharsetName * \u8F6C\u6362\u540E\u7684\u7F16\u7801 * @return \u7ECF\u8FC7\u7F16\u7801\u8F6C\u6362\u540E\u7684\u5B57\u7B26\u4E32 */ private static String conversionStr(String str, String charsetName, String toCharsetName) { try { str = new String(str.getBytes(charsetName), toCharsetName); } catch (UnsupportedEncodingException ex) { System.out.println("\u5B57\u7B26\u4E32\u7F16\u7801\u8F6C\u6362\u5F02\u5E38\uFF1A" + ex.getMessage()); } return str; } public static void main(String[] args) throws IOException { String str="\u4F60\u8FD8\u597D\u5417"; String encoder=URLEncoder.encode(str,"utf-8"); System.out.println(encoder); String decode=URLDecoder.decode(encoder,"utf-8"); System.out.println(decode); Chinesetoen chinesetoen=new Chinesetoen(); System.out.println(chinesetoen.getAllFirstLetter(decode).toUpperCase()); } }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
涌
贡献的其它代码
(
10
)
.
iptables自动屏蔽访问网站频繁的IP
.
实现数组的反转
.
判断一个点是否在一个复杂多边形的内部
.
杀死某个任务 不在hive shell中执行
.
Java API 读取HDFS目录下的所有文件
.
判断一个字符串中是否包含另一个字符串数组或列表中的
.
获取汉字首字母
.
KeyWordHelper-关键字提取类
.
视频
.
查询所有表的外键约束
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3