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

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

(二)源代码
   首先在工具栏上拖放一个动态Table控件 到界面。然后开始编写代码。我修改了一下代码。 因此数据访问层、界面层什么的都在一起。在实际的开发过程中是要分层进行封装的。这里就不一一阐述了。
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Configuration;
None.gif
using  System.Text;
None.gif
None.gif
public  partial  class  Default1 : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
InBlock.gif
InBlock.gif    
protected void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        InitWord();
//创建表格
InBlock.gif
        ShowList();//填充数据
ExpandedSubBlockEnd.gif
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
///创建表格从A-Z
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    void InitWord()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for (int i = 0; i < 26; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string text = ((char)('A' + i)).ToString();
InBlock.gif            Label lb 
= new Label();
InBlock.gif            lb.Text 
= text;
InBlock.gif            lb.Font.Bold 
= true;
InBlock.gif            lb.ForeColor 
= Color.DarkOrange;
InBlock.gif            PlaceHolder ph 
= new PlaceHolder();//这个控件用来动态按格式加载其他控件
InBlock.gif
            ph.ID = "ph" + i.ToString();
InBlock.gif
InBlock.gif            TableCell tc1 
= new TableCell();
InBlock.gif            TableCell tc2 
= new TableCell();
InBlock.gif            tc1.Controls.Add(lb);
InBlock.gif            tc2.Controls.Add(ph);
InBlock.gif            TableRow tr 
= new TableRow();
InBlock.gif            tr.Cells.Add(tc1);
InBlock.gif            tr.Cells.Add(tc2);
InBlock.gif            tr.Cells[
0].Width = 15;
InBlock.gif            tr.Cells[
1].Width = 600;
InBlock.gif            tr.BorderWidth 
= 1;
InBlock.gif            Table1.Rows.Add(tr);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
///填充数据
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    void ShowList()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string strconn = "server=.;database=kaba;uid=sa;pwd=";
InBlock.gif        
string strcomm = "select pro_id,pro_name from products";
InBlock.gif        SqlConnection conn 
= new SqlConnection(strconn);
InBlock.gif        SqlCommand comm 
= new SqlCommand(strcomm, conn);
InBlock.gif        SqlDataAdapter da 
= new SqlDataAdapter(comm);
InBlock.gif        DataSet ds 
= new DataSet();
InBlock.gif        da.Fill(ds);
InBlock.gif
InBlock.gif        
foreach (DataRow dw in ds.Tables[0].Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string ChineseLetter = dw["pro_name"].ToString().Substring(01);
InBlock.gif            
char ch;
InBlock.gif            Encoding gb2312 
= Encoding.GetEncoding("gb2312");
InBlock.gif            
byte[] unicodeBytes = Encoding.Unicode.GetBytes(ChineseLetter);
InBlock.gif            
byte[] gb2312Bytes = Encoding.Convert(Encoding.Unicode, gb2312, unicodeBytes);
InBlock.gif
InBlock.gif            
if (gb2312Bytes.Length == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string EnglishLetter = GetX(Convert.ToInt32(String.Format("{0:D2}", Convert.ToInt16(gb2312Bytes[0]) - 160+ String.Format("{0:D2}", Convert.ToInt16(gb2312Bytes[1]) - 160)));
InBlock.gif                ch 
= char.Parse(EnglishLetter);
ExpandedSubBlockEnd.gif            }

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

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ch 
= Char.Parse(ChineseLetter);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 通过字节码得到首字母.
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="GBCode">字节码</param>
ExpandedSubBlockEnd.gif    
/// <returns>首字母</returns>

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

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/zhouxiaxue/archive/2007/01/31/635872.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值