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

今天特把以前程序开发中的一段代码翻出来,方便大家一起学习、交流。
(一)效果如下:

(二)源代码
   首先在工具栏上拖放一个动态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 "";
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值