C#中 利用汉字在计算机里面的编码来得到汉字的首拼音 及 阿拉伯数字

来源:互联网 发布:sjf调度算法代码 编辑:程序博客网 时间:2024/05/16 01:36

using Microsoft.VisualBasic;

 

public static string GetSpell(string strText)
{
    string myStr = string.Empty;
    for (int i = 0; i < strText.Length; i++)
    {
        myStr += GetFirstSpell(strText.Substring(i, 1));
    }
    return myStr;
}

 

private static string GetFirstSpell(string 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 "*";
    }
    else
    {
        return cnChar;
    }
}

 

private void textBox1_TextChanged(object sender, EventArgs e)
{
    textBox2.Text = "";
    textBox3.Text = "";
    textBox4.Text = "";

    if (textBox1.Text == "")
        listBox1.Visible = false;
    else
        listBox1.Visible = true;

 

    //以下是拼音檢索

    string strsql = "SELECT id,name FROM [PCMIS].[dbo].[DepartmentData] group by id,name ";
    DataTable dtb = new DataTable();
    dtb = SQL.command(strsql).Tables[0];

    string[][] m_list_2 = new string[2][];
    string m_list_1 = "";
    string[] m_list;

    m_list_2[0] = new string[dtb.Rows.Count];
    m_list_2[1] = new string[dtb.Rows.Count];
    for (int i = 0; i < dtb.Rows.Count; i++)
    {
        m_list_2[0][i] = dtb.Rows[i][0].ToString();
        m_list_2[1][i] = dtb.Rows[i][1].ToString();
        m_list_1 += m_list_2[0][i] + " " + m_list_2[1][i] + ",";
    }

 

    //繁简转换

    string s_list = Strings.StrConv(m_list_1, VbStrConv.SimplifiedChinese, 0);

   

    m_list = s_list.Split(',');

    this.listBox1.Items.Clear();
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("[^a-zA-Z0-9/r/n]+");
    if (!reg.Match(textBox1.Text.ToString()).Success)
    {
        //遍历ArrayList中的所有道路信息
        foreach (object o in m_list)
        {
            //获得道路名称各汉字拼音首字母缩写,及阿拉伯数字
            string strRoadName = GetSpell(o.ToString()).ToLower();
            string strtxtRoadName = textBox1.Text.ToLower();
            //根据拼音进行匹配(利用Contain和Substring函数进行判定)
            if (strRoadName.Contains(strtxtRoadName) && strtxtRoadName.Substring(0, strtxtRoadName.Length) == strtxtRoadName)
            {
                listBox1.Items.Add(o);
            }
        }
    }
    else
    {
        //当TextBox为空时显示所有数据
        return;     
    }
}

原创粉丝点击