汉字获取首字母拼音
//获取字典 private Dictionary<string, string> LoadPinYin(HttpContext context) { string link = context.Request.QueryString["link"]; Dictionary<string, string> dic = new Dictionary<string, string>(); DataTable dt = GetData();//获取数据库数据 foreach (DataRow row in dt.Rows) { if (row["idsName"].ToString() == "" || row["idsName"].ToString() == "其它") continue; string pinyin = ChineseCap(row["idsName"].ToString().Substring(0, 1)); if (dic.ContainsKey(pinyin)) { dic[pinyin] += string.Format("<a href='{1}' title='{2}'>{0}</a>", clsCommon.leftx(row["idsName"].ToString(), 10), Global.RewriteStaticUrl(link + "?idsid=" + row["idsID"].ToString()), row["idsName"].ToString()); } else { dic.Add(pinyin, string.Format("<a href='{1}' title='{2}'>{0}</a>", clsCommon.leftx(row["idsName"].ToString(), 10), Global.RewriteStaticUrl(link + "?idsid=" + row["idsID"].ToString()), row["idsName"].ToString())); } } return dic; } //转换 public string ChineseCap(string ChineseStr) { string Capstr = string.Empty; byte[] ZW = new byte[2]; long ChineseStr_int; string CharStr, ChinaStr = ""; for (int i = 0; i <= ChineseStr.Length - 1; i++) { CharStr = ChineseStr.Substring(i, 1).ToString(); ZW = System.Text.Encoding.Default.GetBytes(CharStr); // 得到汉字符的字节数组 if (ZW.Length == 2) { int i1 = (short)(ZW[0]); int i2 = (short)(ZW[1]); ChineseStr_int = i1 * 256 + i2; if ((ChineseStr_int >= 45217) && (ChineseStr_int <= 45252)) { ChinaStr = "A"; } else if ((ChineseStr_int >= 45253) && (ChineseStr_int <= 45760)) { ChinaStr = "B"; } else if ((ChineseStr_int >= 45761) && (ChineseStr_int <= 46317)) { ChinaStr = "C"; } else if ((ChineseStr_int >= 46318) && (ChineseStr_int <= 46825)) { ChinaStr = "D"; } else if ((ChineseStr_int >= 46826) && (ChineseStr_int <= 47009)) { ChinaStr = "E"; } else if ((ChineseStr_int >= 47010) && (ChineseStr_int <= 47296)) { ChinaStr = "F"; } else if ((ChineseStr_int >= 47297) && (ChineseStr_int <= 47613)) { ChinaStr = "G"; } else if ((ChineseStr_int >= 47614) && (ChineseStr_int <= 48118)) { ChinaStr = "H"; } else if ((ChineseStr_int >= 48119) && (ChineseStr_int <= 49061)) { ChinaStr = "J"; } else if ((ChineseStr_int >= 49062) && (ChineseStr_int <= 49323)) { ChinaStr = "K"; } else if ((ChineseStr_int >= 49324) && (ChineseStr_int <= 49895)) { ChinaStr = "L"; } else if ((ChineseStr_int >= 49896) && (ChineseStr_int <= 50370)) { ChinaStr = "M"; } else if ((ChineseStr_int >= 50371) && (ChineseStr_int <= 50613)) { ChinaStr = "N"; } else if ((ChineseStr_int >= 50614) && (ChineseStr_int <= 50621)) { ChinaStr = "O"; } else if ((ChineseStr_int >= 50622) && (ChineseStr_int <= 50905)) { ChinaStr = "P"; } else if ((ChineseStr_int >= 50906) && (ChineseStr_int <= 51386)) { ChinaStr = "Q"; } else if ((ChineseStr_int >= 51387) && (ChineseStr_int <= 51445)) { ChinaStr = "R"; } else if ((ChineseStr_int >= 51446) && (ChineseStr_int <= 52217)) { ChinaStr = "S"; } else if ((ChineseStr_int >= 52218) && (ChineseStr_int <= 52697)) { ChinaStr = "T"; } else if ((ChineseStr_int >= 52698) && (ChineseStr_int <= 52979)) { ChinaStr = "W"; } else if ((ChineseStr_int >= 52980) && (ChineseStr_int <= 53640)) { ChinaStr = "X"; } else if ((ChineseStr_int >= 53689) && (ChineseStr_int <= 54480)) { ChinaStr = "Y"; } else if ((ChineseStr_int >= 54481) && (ChineseStr_int <= 55289)) { ChinaStr = "Z"; } } else { Capstr = ChineseStr; break; } Capstr = Capstr + ChinaStr; } return Capstr; }