【高手进呐】如何获得 单个汉字的拼音首字母啊 有什么更好的方法嘛

happyer_longlong 2009-05-21 04:08:47
前端时间 研究如何获得汉字首字母的问题 颇有心得 无奈 csdn的个人博客总打不开 没法写
忘各位大侠 交流交流思想 等博客能用了就发表啊
【注】gb2312 字符集是不够的

...全文
361 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
Leonxx 2011-04-25
  • 打赏
  • 举报
回复
试过了,多音字的问题不知道该如何解决。例如“重庆”得到的结果是"ZQ"。
happyer_longlong 2009-05-21
  • 打赏
  • 举报
回复
说来说去都是那一个方法 楼上的方法基本上都相同啊 都出自一个版本理论上
【注】gb2312 不到8000个 字符是不够的
  • 打赏
  • 举报
回复
//// <summary>
/// 通过字节码得到首字母.
/// </summary>
/// <param name="GBCode">字节码</param>
/// <returns>首字母</returns>
private String GetInf(int GBCode)
{
if (GBCode < 1601 || GBCode > 5589) return "";
if (GBCode >= 1601 && GBCode < 1637) return "A";
if (GBCode >= 1637 && GBCode < 1833) return "B";
if (GBCode >= 1833 && GBCode < 2078) return "C";
if (GBCode >= 2078 && GBCode < 2274) return "D";
if (GBCode >= 2274 && GBCode < 2302) return "E";
if (GBCode >= 2302 && GBCode < 2433) return "F";
if (GBCode >= 2433 && GBCode < 2594) return "G";
if (GBCode >= 2594 && GBCode < 2787) return "H";
if (GBCode >= 2787 && GBCode < 3106) return "J";
if (GBCode >= 3106 && GBCode < 3212) return "K";
if (GBCode >= 3212 && GBCode < 3472) return "L";
if (GBCode >= 3472 && GBCode < 3635) return "M";
if (GBCode >= 3635 && GBCode < 3722) return "N";
if (GBCode >= 3722 && GBCode < 3730) return "O";
if (GBCode >= 3730 && GBCode < 3858) return "P";
if (GBCode >= 3858 && GBCode < 4027) return "Q";
if (GBCode >= 4027 && GBCode < 4086) return "R";
if (GBCode >= 4086 && GBCode < 4390) return "S";
if (GBCode >= 4390 && GBCode < 4558) return "T";
if (GBCode >= 4558 && GBCode < 4684) return "W";
if (GBCode >= 4684 && GBCode < 4925) return "X";
if (GBCode >= 4925 && GBCode < 5249) return "Y";
if (GBCode >= 5249 && GBCode <= 5589) return "Z";
return "";
}
}
delacrxoix_xu 2009-05-21
  • 打赏
  • 举报
回复
学习了~~~这么多方法
CqCoder 2009-05-21
  • 打赏
  • 举报
回复
关注··帮顶
我姓区不姓区 2009-05-21
  • 打赏
  • 举报
回复
我还是比较倾向于微软提供的一个dll
happyer_longlong 2009-05-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wujinjian2008n 的回复:]
引用 1 楼 zgke 的回复:
两种方式..1种计算方式..可能有些字出不来
查表方式..需要定义好一个字典表.


up
[/Quote]
说的好
happyer_longlong 2009-05-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 coconutyf 的回复:]
去这看看,用区位码能获得
http://www.diybl.com/course/4_webprogram/asp.net/netjs/20090309/160337.html
[/Quote]
太麻烦了吧 还用正则 吓人啊
happyer_longlong 2009-05-21
  • 打赏
  • 举报
回复
hsnhsn0916 2009-05-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sayoath 的回复:]
C# code
#region 获取汉字第一个拼音
/**/
/// <summary>
/// 获取汉字第一个拼音
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
static public string getSpells(string input)
{
int len = input.Length;
string reVal = "";
for (int i = 0; i < len; i++)
{

[/Quote]
5楼这方法不错
deknight 2009-05-21
  • 打赏
  • 举报
回复

#region =中文转首字字母=
public static string GetPYString(string str)
{
string tempStr = "";
foreach (char c in str)
{
//if ((int)c >= 33 && (int)c <= 126)
if ((int)c >= 32 && (int)c <= 126)
{//字母和符号原样保留
tempStr += c.ToString();
}
else
{//累加拼音声母
tempStr += GetPYChar(c.ToString());
}
}
return tempStr;
}
#endregion

#region =中文字母对照=
public static string GetPYChar(string c)
{
byte[] array = new byte[2];
array = System.Text.Encoding.Default.GetBytes(c);
int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));

if (i < 0xB0A1) return "*";
if (i < 0xB0C5) return "a";
if (i < 0xB2C1) return "b";
if (i < 0xB4EE) return "c";
if (i < 0xB6EA) return "d";
if (i < 0xB7A2) return "e";
if (i < 0xB8C1) return "f";
if (i < 0xB9FE) return "g";
if (i < 0xBBF7) return "h";
if (i < 0xBFA6) return "g";
if (i < 0xC0AC) return "k";
if (i < 0xC2E8) return "l";
if (i < 0xC4C3) return "m";
if (i < 0xC5B6) return "n";
if (i < 0xC5BE) return "o";
if (i < 0xC6DA) return "p";
if (i < 0xC8BB) return "q";
if (i < 0xC8F6) return "r";
if (i < 0xCBFA) return "s";
if (i < 0xCDDA) return "t";
if (i < 0xCEF4) return "w";
if (i < 0xD1B9) return "x";
if (i < 0xD4D1) return "y";
if (i < 0xD7FA) return "z";
return "*";
}
#endregion
ztenv 2009-05-21
  • 打赏
  • 举报
回复
网上有很多代码,
wuyq11 2009-05-21
  • 打赏
  • 举报
回复
sqlserver和C#里都可实现
参考
参考
liuy052 2009-05-21
  • 打赏
  • 举报
回复
学习了。
coconutyf 2009-05-21
  • 打赏
  • 举报
回复
去这看看,用区位码能获得
http://www.diybl.com/course/4_webprogram/asp.net/netjs/20090309/160337.html
sayoath 2009-05-21
  • 打赏
  • 举报
回复

#region 获取汉字第一个拼音
/**/
/// <summary>
/// 获取汉字第一个拼音
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
static public string getSpells(string input)
{
int len = input.Length;
string reVal = "";
for (int i = 0; i < len; i++)
{
reVal += getSpell(input.Substring(i, 1));
}
return reVal;
}
static public string getSpell(string cn)
{
byte[] arrCN = Encoding.Default.GetBytes(cn);
if (arrCN.Length > 1)
{
int area = (short)arrCN[0];
int pos = (short)arrCN[1];
int code = (area << 8) + pos;
int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
for (int i = 0; i < 26; i++)
{
int max = 55290;
if (i != 25) max = areacode[i + 1];
if (areacode[i] <= code && code < max)
{
return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
}
}
return "?";
}
else return cn;

}
#endregion

我要50分嘿嘿!
wujinjian2008n 2009-05-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zgke 的回复:]
两种方式..1种计算方式..可能有些字出不来
查表方式..需要定义好一个字典表.
[/Quote]

up
andrewbornglobal 2009-05-21
  • 打赏
  • 举报
回复
帖子不错
hecker728 2009-05-21
  • 打赏
  • 举报
回复
帮顶,学习
zgke 2009-05-21
  • 打赏
  • 举报
回复
两种方式..1种计算方式..可能有些字出不来
查表方式..需要定义好一个字典表.

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧