获取输入字符串的首字母(c#)

最近做一个项目,需要调用分类名称的首字母做索引。。但如果每次添加分类都手动输入首字母又太麻烦,上网翻了下资料,只发现了asp的代码,没有asp.net的。呵呵。就自己随手改了下,丢出来给新手用。

声明:没什么技术含量,高手免入。哈哈。

//获取输入字符的ascii编码
public int asc(char mm)
{
byte[] aa = System.Text.Encoding.Default.GetBytes(mm.ToString());
int pp = 0;
if (aa.Length == 1)
{
pp =(int)aa[0];
}
else
{
for (int i = 0; i < aa.Length; i+=2)
{
pp = (int)aa[i];
pp = pp * 256 + aa[i + 1] - 65536;
}
}
return pp;
}

//获取汉字或英文的首字母
public char FirstChar(char InputChar)
{
int tmp=65536+asc(InputChar);
string getpychar = "";
if(tmp>=45217&&tmp<=45252)
{
getpychar= "A";
}
else if(tmp>=45253&&tmp<=45760)
{
getpychar= "B";
}
else if(tmp>=45761&&tmp<=46317)
{
getpychar= "C";
}
else if(tmp>=46318&&tmp<=46825)
{
getpychar= "D";
}
else if(tmp>=46826&&tmp<=47009)
{
getpychar= "E";
}
else if(tmp>=47010&&tmp<=47296)
{
getpychar= "F";
}
else if(tmp>=47297&&tmp<=47613)
{
getpychar= "G";
}
else if(tmp>=47614&&tmp<=48118)
{
getpychar= "H";
}
else if(tmp>=48119&&tmp<=49061)
{
getpychar= "J";
}
else if(tmp>=49062&&tmp<=49323)
{
getpychar= "K";
}
else if(tmp>=49324&&tmp<=49895)
{
getpychar= "L";
}
else if(tmp>=49896&&tmp<=50370)
{
getpychar= "M";
}
else if(tmp>=50371&&tmp<=50613)
{
getpychar= "N";
}
else if(tmp>=50614&&tmp<=50621)
{
getpychar= "O";
}
else if(tmp>=50622&&tmp<=50905)
{
getpychar= "P";
}
else if(tmp>=50906&&tmp<=51386)
{
getpychar= "Q";
}
else if(tmp>=51387&&tmp<=51445)
{
getpychar= "R";
}
else if(tmp>=51446&&tmp<=52217)
{
getpychar= "S";
}
else if(tmp>=52218&&tmp<=52697)
{
getpychar= "T";
}
else if(tmp>=52698&&tmp<=52979)
{
getpychar= "W";
}
else if ((tmp >= 52980 && tmp <= 53640) || tmp == 63182)
{
getpychar= "X";
}


else if(tmp>=53689&&tmp<=54480)
{
getpychar= "Y";
}
else if(tmp>=54481&&tmp<=62289)
{

getpychar= "Z";
}
else
//如果不是中文,则不处理
{
getpychar=InputChar.ToString();
}
return Convert.ToChar(getpychar);
}
  

猜你喜欢

转载自blog.csdn.net/dreamskys/article/details/926002
今日推荐