首页 新闻 会员 周边

asp.net mvc3 根据拼音字母索引

0
[已解决问题] 解决于 2013-08-27 13:11

现在我要实现这样的字母索引列表,有一个表GameInfo(游戏信息表),其中有一个字段(FirstChar)是存放游戏名首字母的。问题是:在填写游戏信息的时候,我想根据游戏名称,获取首字母,然后存入FirstChar字段。。。。然后根据首字母,获取相关游戏名称,显示在页面。在网上找到一个在数据库中自定义的标量函数,但是他只能根据一个汉字获取首字母,我的数据库用的是存储过程。。。现请高人指点,这个思路是否正确,然后给予相关的意见或解决方法。。。

"程序猿"~在路上的主页 "程序猿"~在路上 | 初学一级 | 园豆:162
提问于:2013-08-26 23:57
< >
分享
最佳答案
0

//获取汉字拼音的第一个字母
static public string GetChineseSpell(string strText)
{
int len = strText.Length;
string myStr = "";
for (int i = 0; i < len; i++)
{
myStr += getSpell(strText.Substring(i, 1));
}
return myStr;
}

static public string[] GetChineseSpell(string[] strText)
{
int len = strText.Length;
string[] myStr = null;
for (int i = 0; i < len; i++)
{
myStr[i] = getSpell(strText[i]);
}
return myStr;
}

static public string getSpell(string cnChar)
{
byte[] arrCN = System.Text.Encoding.Default.GetBytes(cnChar);
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 System.Text.Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
}
}
return "*";
}
else return cnChar;
}

用的时候直接调用GetChineseSpell即可

奖励园豆:5
ExploreForward | 初学一级 |园豆:18 | 2013-08-27 09:33
其他回答(1)
0

我不得告诉你,有个库叫NPinyin。

幻天芒 | 园豆:37175 (高人七级) | 2013-08-27 01:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册