.NET中如何取得汉字或者拼音首字母(附源代码)

来源:互联网 发布:商品期货行情数据接口 编辑:程序博客网 时间:2024/06/06 13:13
今天特把以前程序开发中的一段代码翻出来,方便大家一起学习、交流。
(一)效果如下:

(二)源代码
   首先在工具栏上拖放一个动态Table控件到界面。然后开始编写代码。我修改了一下代码。 因此数据访问层、界面层什么的都在一起。在实际的开发过程中是要分层进行封装的。这里就不一一阐述了。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Text;

public partial class Default1 : System.Web.UI.Page
{

    
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;

    
protected void Page_Load(object sender, System.EventArgs e)
    
{
        InitWord();
//创建表格
        ShowList();//填充数据
    }

    
/// <summary>
    
///创建表格从A-Z
    
/// </summary>

    void InitWord()
    
{
        
for (int i = 0; i < 26; i++)
        
{
            
string text = ((char)('A' + i)).ToString();
            Label lb 
= new Label();
            lb.Text 
= text;
            lb.Font.Bold 
= true;
            lb.ForeColor 
= Color.DarkOrange;
            PlaceHolder ph 
= new PlaceHolder();//这个控件用来动态按格式加载其他控件
            ph.ID = "ph" + i.ToString();

            TableCell tc1 
= new TableCell();
            TableCell tc2 
= new TableCell();
            tc1.Controls.Add(lb);
            tc2.Controls.Add(ph);
            TableRow tr 
= new TableRow();
            tr.Cells.Add(tc1);
            tr.Cells.Add(tc2);
            tr.Cells[
0].Width = 15;
            tr.Cells[
1].Width = 600;
            tr.BorderWidth 
= 1;
            Table1.Rows.Add(tr);
        }


    }

    
/// <summary>
    
///填充数据
    
/// </summary>

    void ShowList()
    
{
        
string strconn = "server=.;database=kaba;uid=sa;pwd=";
        
string strcomm = "select pro_id,pro_name from products";
        SqlConnection conn 
= new SqlConnection(strconn);
        SqlCommand comm 
= new SqlCommand(strcomm, conn);
        SqlDataAdapter da 
= new SqlDataAdapter(comm);
        DataSet ds 
= new DataSet();
        da.Fill(ds);

        
foreach (DataRow dw in ds.Tables[0].Rows)
        
{
            
string ChineseLetter = dw["pro_name"].ToString().Substring(01);
            
char ch;
            Encoding gb2312 
= Encoding.GetEncoding("gb2312");
            
byte[] unicodeBytes = Encoding.Unicode.GetBytes(ChineseLetter);
            
byte[] gb2312Bytes = Encoding.Convert(Encoding.Unicode, gb2312, unicodeBytes);

            
if (gb2312Bytes.Length == 2)
            
{
                
string EnglishLetter = GetX(Convert.ToInt32(String.Format("{0:D2}", Convert.ToInt16(gb2312Bytes[0]) - 160+ String.Format("{0:D2}", Convert.ToInt16(gb2312Bytes[1]) - 160)));
                ch 
= char.Parse(EnglishLetter);
            }

            
else
            
{
                
int change = (int)char.Parse(ChineseLetter);
                
if (change >= 97 && change <= 122)
                
{
                    change 
= change - 32;
                    ch 
= Convert.ToChar(change);
                }

                
else
                
{
                    ch 
= Char.Parse(ChineseLetter);
                }

            }

            
int index = (int)ch - 'A';
            LinkButton lb 
= new LinkButton();
            lb.Text 
= dw["pro_name"].ToString() + "  ";
            lb.Font.Bold 
= true;
            lb.ForeColor 
= Color.Blue;
            lb.ToolTip 
= dw["pro_id"].ToString();
            PlaceHolder ph 
= Table1.Rows[index].Cells[1].FindControl("ph" + index.ToString()) as PlaceHolder;
            ph.Controls.Add(lb);
        }

    }

    
/// <summary>
    
/// 通过字节码得到首字母.
    
/// </summary>
    
/// <param name="GBCode">字节码</param>
    
/// <returns>首字母</returns>

    private String GetX(int GBCode)
    
{
        
if (GBCode < 1601 || GBCode > 5589return "";
        
if (GBCode >= 1601 && GBCode < 1637return "A";
        
if (GBCode >= 1637 && GBCode < 1833return "B";
        
if (GBCode >= 1833 && GBCode < 2078return "C";
        
if (GBCode >= 2078 && GBCode < 2274return "D";
        
if (GBCode >= 2274 && GBCode < 2302return "E";
        
if (GBCode >= 2302 && GBCode < 2433return "F";
        
if (GBCode >= 2433 && GBCode < 2594return "G";
        
if (GBCode >= 2594 && GBCode < 2787return "H";
        
if (GBCode >= 2787 && GBCode < 3106return "J";
        
if (GBCode >= 3106 && GBCode < 3212return "K";
        
if (GBCode >= 3212 && GBCode < 3472return "L";
        
if (GBCode >= 3472 && GBCode < 3635return "M";
        
if (GBCode >= 3635 && GBCode < 3722return "N";
        
if (GBCode >= 3722 && GBCode < 3730return "O";
        
if (GBCode >= 3730 && GBCode < 3858return "P";
        
if (GBCode >= 3858 && GBCode < 4027return "Q";
        
if (GBCode >= 4027 && GBCode < 4086return "R";
        
if (GBCode >= 4086 && GBCode < 4390return "S";
        
if (GBCode >= 4390 && GBCode < 4558return "T";
        
if (GBCode >= 4558 && GBCode < 4684return "W";
        
if (GBCode >= 4684 && GBCode < 4925return "X";
        
if (GBCode >= 4925 && GBCode < 5249return "Y";
        
if (GBCode >= 5249 && GBCode <= 5589return "Z";
        
return "";
    }

}