- public class ChineseLetter {
-
-
private static final int GB_SP_DIFF = 160;
-
-
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 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' };
-
-
-
public static String getFirstLetter(String oriStr) {
- String str = oriStr.toLowerCase();
-
StringBuffer buffer = new StringBuffer();
-
char ch;
-
char[] temp;
-
for (int i = 0; i < str.length(); i++) {
- ch = str.charAt(i);
-
temp = new char[] { ch };
-
byte[] uniCode = new String(temp).getBytes();
-
if (uniCode[0] < 128 && uniCode[0] > 0) {
- buffer.append(temp);
-
} else {
- buffer.append(convert(uniCode));
- }
- }
-
return buffer.toString();
- }
-
-
-
public 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;
- }
-
-
public static void main(String[] args) {
-
System.out.println(ChineseLetter.getFirstLetter("我是中国人"));
- }
- }
public class ChineseLetter {
// 国标码和区位码转换常量
private static final int GB_SP_DIFF = 160;
// 存放国标一级汉字不同读音的起始区位码
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 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' };
// 获取一个字符串的拼音码
public static String getFirstLetter(String oriStr) {
String str = oriStr.toLowerCase();
StringBuffer buffer = new StringBuffer();
char ch;
char[] temp;
for (int i = 0; i < str.length(); i++) { // 依次处理str中每个字符
ch = str.charAt(i);
temp = new char[] { ch };
byte[] uniCode = new String(temp).getBytes();
if (uniCode[0] < 128 && uniCode[0] > 0) { // 非汉字
buffer.append(temp);
} else {
buffer.append(convert(uniCode));
}
}
return buffer.toString();
}
// 获取一个汉字的首字母
public 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;
}
public static void main(String[] args) {
System.out.println(ChineseLetter.getFirstLetter("我是中国人"));
}
}
输出结果:
wszgr
分享到:
相关推荐
主要介绍了分享一段PHP制作的中文拼音首字母工具类的代码,非常的实用,代码很简单。 注: 英文的字串:不变返回(包括数字) eg .abc123 => abc123 中文字符串:返回拼音首字符 eg. 测试字符串 => CSZFC 中英混合...
网上的方法有不少,都是一样的原理,按照...php /*** Modified by http://iulog.com @ 2013-05-07* 修复二分法查找方法* 汉字拼音首字母工具类* 注: 英文的字串:不变返回(包括数字) eg .abc123 => abc123* 中
主要介绍了分享一段PHP制作的中文拼音首字母工具类的代码,非常的实用,代码很简单。 注: 英文的字串:不变返回(包括数字) eg .abc123 => abc123 中文字符串:返回拼音首字符 eg. 测试字符串 => ...
!! 复制代码 代码如下: <?.../** * 汉字拼音首字母工具类 * 注: 英文的字串:不变返回(包括数字) eg .abc123 => abc123 * 中文字符串:返回拼音首字符 eg....* 中英混合串: 返回拼音首字符和...* //获取首字母
在首字哈希表中查询次字的拼音首字母,根据双字以上词的数量决定是否按次字拼音首字母散列到次字首字母哈希表中,以此决定次字的查询方式.在兼顾空间复杂度的同时,缩小次字查询范围能较大幅度地提升高频词的次字的...
目前的下拉框dropdownlist、select就是简单的选择,如果遇到内容过多会很难找出想找的选项,我用简单的js文件将现在的下拉框改成下拉模糊搜索框,支持汉字拼音首字母模糊匹配和字符串字串匹配,本代码的强大在于扩展...
中文字符串转换为拼音或者拼音首字母的辅助类(PinYinUtil.cs) 随机汉字辅助类(RandomChinese.cs) 反射操作辅助类,如获取或设置字段、属性的值等反射信息。(ReflectionUtil.cs) 注册表操作辅助类(RegistryHelper...
10. 中文字符串转换为拼音或者拼音首字母的辅助类(PinYinUtil.cs) 11. 随机汉字辅助类(RandomChinese.cs) 12. 反射操作辅助类,如获取或设置字段、属性的值等反射信息。(ReflectionUtil.cs) 13. 注册表操作辅助...
10.中文字符串转换为拼音或者拼音首字母的辅助类(PinYinUtil.cs) 11.随机汉字辅助类(RandomChinese.cs) 12.反射操作辅助类,如获取或设置字段、属性的值等反射信息。(ReflectionUtil.cs) 13.注册表操作辅助类...