参考 http://hi.baidu.com/ggukknvsdcbbgxq/item/0ef8ea9d82479b49f142155b
using System.Text.RegularExpressions;
//利用正则表达式验证汉字和英文,阿拉伯数字
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("^[\u4e00-\u9fa5]+$ || ^[a-zA-Z0-9]+$"); //验证汉字和英文,阿拉伯数字
if (reg.Match(textBox5.Text.ToString()).Success)
{
//遍历ArrayList中的所有信息
foreach (object o in m_list)
{
//获得各汉字拼音首字母缩写,及阿拉伯数字,GetSpell是自己写的函数
string strRoadName = GetSpell(o.ToString()).ToLower();
string strtxtRoadName = textBox5.Text.ToLower();
//根据拼音进行匹配(利用Contain和Substring函数进行判定)
if ( strtxtRoadName.Substring(0, strtxtRoadName.Length) == strtxtRoadName)
{
listBox2.Items.Add(o);
}
}
}
else
{
//当TextBox为空时显示所有数据
return;
}
public static string GetSpell(string strText) //自已写的函数
{
string myStr = string.Empty;
for (int i = 0; i < strText.Length; i++)
{
//代码效率高的,缺点就是只支持GB2312,不支持GBK(有很多偏僻的字读不出来)
myStr += GetFirstSpell(strText.Substring(i, 1));
//代码效率低,但支持GBK
myStr += GetGbkX(strText.Substring(i, 1));
}
return myStr;
}
private static string GetFirstSpell(string cnChar) //自己写的函数,下面的代码是效率高的,缺点就是只支持GB2312,不支持GBK(有很多偏僻的字读不出来)
{
//byte[] arrCn = Encoding.Default.GetBytes(cnChar);
byte[] arrCn = Encoding.Default.GetBytes(cnChar);
if (arrCn.Length > 1)
{
int area = (short)arrCn[0];
int pos = (short)arrCn[1];
int code = (area << 8) + pos;
int[] bmcode =
{
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 = bmcode[i + 1];
if (bmcode[i] <= code && code < max)
return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
}
return "*";
}
return cnChar;
}
//下面这个支持GBK,但是效率不是很高
private static string GetGbkX(string str)
{
if (str.CompareTo("吖") < 0) return str;
if (str.CompareTo("八") < 0) return "A";
if (str.CompareTo("嚓") < 0) return "B";
if (str.CompareTo("咑") < 0) return "C";
if (str.CompareTo("妸") < 0) return "D";
if (str.CompareTo("发") < 0) return "E";
if (str.CompareTo("旮") < 0) return "F";
if (str.CompareTo("铪") < 0) return "G";
if (str.CompareTo("讥") < 0) return "H";
if (str.CompareTo("咔") < 0) return "J";
if (str.CompareTo("垃") < 0) return "K";
if (str.CompareTo("嘸") < 0) return "L";
if (str.CompareTo("拏") < 0) return "M";
if (str.CompareTo("噢") < 0) return "N";
if (str.CompareTo("妑") < 0) return "O";
if (str.CompareTo("七") < 0) return "P";
if (str.CompareTo("亽") < 0) return "Q";
if (str.CompareTo("仨") < 0) return "R";
if (str.CompareTo("他") < 0) return "S";
if (str.CompareTo("哇") < 0) return "T";
if (str.CompareTo("夕") < 0) return "W";
if (str.CompareTo("丫") < 0) return "X";
if (str.CompareTo("帀") < 0) return "Y";
if (str.CompareTo("咗") < 0) return "Z";
return str;
}
//table of the constant list(以下是拼音的代表号)
// 'A'; //45217..45252
// 'B'; //45253..45760
// 'C'; //45761..46317
// 'D'; //46318..46825
// 'E'; //46826..47009
// 'F'; //47010..47296
// 'G'; //47297..47613
// 'H'; //47614..48118
// 'J'; //48119..49061
// 'K'; //49062..49323
// 'L'; //49324..49895
// 'M'; //49896..50370
// 'N'; //50371..50613
// 'O'; //50614..50621
// 'P'; //50622..50905
// 'Q'; //50906..51386
// 'R'; //51387..51445
// 'S'; //51446..52217
// 'T'; //52218..52697
//没有U,V
// 'W'; //52698..52979
// 'X'; //52980..53640
// 'Y'; //53689..54480
// 'Z'; //54481..55289