Java获取汉字的大小写拼音码(汉字的拼音首字母)
import java.io.UnsupportedEncodingException; /**
* 获取拼音码
*
* @author xmj
*
*/
public class GetPinyinCode { private final static char[] en_char_Upper = { '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 static int[] posValue = { 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 static int[] posValue2 = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 49062,
49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52980, 53689, 54481, 55290 }; /**
* 获取字符串的大小写拼音码,true为大写
*
* @param strChinese
* @param bUpCase
* @return
*/
public static String getPYIndexStr(String strChinese, boolean bUpCase) {
try {
StringBuffer buffer = new StringBuffer(); byte[] b = strChinese.getBytes("GBK"); int i = 0;
while (i < b.length) {
if ((b[i] & 0xFF) > 128) {
int char1 = b[(i++)] & 0xFF;
char1 <<= 8;
int chart = char1 + (b[i] & 0xFF);
buffer.append(getPYIndexChar((char) chart, bUpCase));
} else {
char c = (char) b[i];
if (!Character.isJavaIdentifierPart(c))
c = '?';
buffer.append(c);
}
i++;
}
return buffer.toString();
} catch (Exception e) {
System.out.println("取中文拼音有错" + e.getMessage());
}
return null;
} /**
* 获取字符
*
* @param strChinese
* @param bUpCase
* @return
*/
private static char getPYIndexChar(char strChinese, boolean bUpCase) {
int charGBK = strChinese;
char result = '~';
if (charGBK > 45216 && charGBK < 55290) {
for (int i = 0; i < 23; i++) {
if (charGBK >= posValue2[i] && charGBK < posValue2[i + 1]) {
result = en_char_Upper[i];
break;
}
}
}
if (!bUpCase)
result = Character.toLowerCase(result);
return result;
} /**
* 获取字符串的大小写拼音码,true为大写
*
* @param str
* @param bUpCase
* @return
*/
public static String getPYIndexStr2(String strChinese, boolean bUpCase) {
if (strChinese == null || strChinese.trim().length() == 0) {
return "";
}
String result = "";
for (int i = 0; i < strChinese.length(); i++) {
try {
result = result + getFirstLetter(strChinese.substring(i, i + 1), bUpCase);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result;
} /**
* 取得给定汉字的首字母,true为大写
*
* @param chinese
* @param bUpCase
* @return
* @throws UnsupportedEncodingException
*/
private static String getFirstLetter(String strChinese, boolean bUpCase) throws UnsupportedEncodingException {
if (strChinese == null || strChinese.trim().length() == 0) {
return "";
}
strChinese = new String(strChinese.getBytes("GB2312"), "ISO8859-1");
if (strChinese.length() > 1) {
int ch_area_code = (int) strChinese.charAt(0) - 160;// 汉字区码
int ch_bit_code = (int) strChinese.charAt(1) - 160; // 汉字位码
int ch_code = ch_area_code * 100 + ch_bit_code; // 汉字区位码
if (ch_code > 1600 && ch_code < 5590) {
for (int i = 0; i < 23; i++) {
if (ch_code >= posValue[i] && ch_code < posValue[i + 1]) {
char a = en_char_Upper[i];
if (!bUpCase)
a = Character.toLowerCase(a);
strChinese = String.valueOf(a);
break;
}
}
} else {
strChinese = new String(strChinese.getBytes("ISO8859"), "GB2312");
strChinese = strChinese.substring(0, 1);
}
}
return strChinese;
} }
测试一下

Java获取汉字的大小写拼音码(汉字的拼音首字母)的更多相关文章
- JAVA将汉字转换为全拼以及返回中文的首字母,将字符串转移为ASCII码
import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.pinyin4j.format.HanyuPinyinCaseT ...
- java 汉语转拼音(全拼,首字母)
import java.util.*; import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.pinyin4j.for ...
- js汉语转拼音(全拼、首字母、拼音首字母)
新建js文件first_alphabet.js // JavaScript Document // 汉字拼音首字母列表 本列表包含了20902个汉字,用于配合 ToChineseSpell //函数使 ...
- 关于java中实现在oracle数据库中实现对中文首字母进行排序的解决方案
首先介绍Oracle 9i新增加的一个系统自带的排序函数 1.按首字母排序 在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值 SCHINESE_RADICAL_M ...
- Java序列化,解决字段为null与序列化后首字母变小写问题
fastjson.jar package com.apt.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.se ...
- java获取Json和http状态码
最近再做接口自动化测试,其中有几个方法比较重要 1.获取http状态码 /* * 返回接口状态码 * */ public static String getHttpCode(String url) { ...
- Java获取字符串的CRC8校验码(由C程序的代码修改为了Java代码)
CRC8算法请百度,我也不懂,这里只是把自己运行成功的结构贴出来了.方法CRC8_Tab这里没有处理,因为我的程序中没有用到. package com.crc; public class CCRC8_ ...
- php 中文转拼音,可以只转首字母,可以设置utf8、gbk
<?php class Pinyin { /** * 默认是gb编码,第二个参数随意设置即为utf8编 * @param type $isInitial 是否只返回首字母 * @return t ...
- java获取汉字拼音首字母 --转载
在项目中要更能根据某些查询条件(比如姓名)的首字母作为条件进行查询,比如查一个叫"李晓明"的人,可以输入'lxm'.写了一个工具类如下: import java.io.Unsupp ...
随机推荐
- Windows下FFMEPG编译
FFMPEG在Windows下编译(MIMO431) /************************************************************************ ...
- npm 报错This is probably not a problem with npm. There is likely additional logging output above.
报错This is probably not a problem with npm. There is likely additional logging output above. 安装了一个插件后 ...
- Exception in thread "main" java.awt.AWTError: Assistive Technology not found: org.GNOME.Accessibilit
系统环境 Ubuntu 20.04 focal 问题分析 该异常出现的原因,从谷歌上可以得到答案 one of the more common causes of this exception is ...
- 转载:Linux图形界面知识(介绍X、X11、GNOME、Xorg、KDE等之间的关系)
转载 http://blog.csdn.net/zhangxinrun/article/details/7332049Linux初学者经常分不清楚linux和X之间,X和Xfree86之间,X和KDE ...
- Solon 1.7 重要发布,更现代感的应用开发框架
相对于 Spring Boot 和 Spring Cloud 的项目 启动快 5 - 10 倍 qps 高 2- 3 倍 运行时内存节省 1/3 ~ 1/2 打包可以缩小到 1/2 ~ 1/10(比如 ...
- GO语言学习——切片二
使用make()函数构造切片 格式: make([]T, size, cap) 其中: T:切片的元素类型 size:切片中元素的数量 cap:切片的容量 切片的本质 切片的本质就是对底层数组的封装, ...
- c++:-2
上节介绍C++的函数介绍:c++:-1,本节学习类与对象 类与对象 定义 类定义 class 类名称 { public: 公有成员(外部接口) private: 私有成员 protected: 保护型 ...
- 【面试普通人VS高手系列】讲一下wait和notify这个为什么要在synchronized代码块中?
一个工作七年的小伙伴,竟然不知道"wait"和"notify"为什么要在Synchronized代码块里面. 好吧,如果屏幕前的你也不知道,请在评论区打上&qu ...
- 力扣算法:125-验证回文串,131-分割回文串---js
LC 125-验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 注:回文串是正着读和反着读都一样的字符串. ...
- 无线:PIN码
PIN码(PIN1),全称Personal Identification Number.就是SIM卡的个人识别密码.手机的PIN码是保护SIM卡的一种安全措施,防止别人盗用SIM卡,如果启用了开机PI ...