.NET中如何取得汉字或者拼音首字母(附源代码)
来源:互联网 发布:商品期货行情数据接口 编辑:程序博客网 时间:2024/06/06 13:13
今天特把以前程序开发中的一段代码翻出来,方便大家一起学习、交流。
(一)效果如下:
(二)源代码
首先在工具栏上拖放一个动态Table控件到界面。然后开始编写代码。我修改了一下代码。 因此数据访问层、界面层什么的都在一起。在实际的开发过程中是要分层进行封装的。这里就不一一阐述了。
(一)效果如下:
(二)源代码
首先在工具栏上拖放一个动态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(0, 1);
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 > 5589) return "";
if (GBCode >= 1601 && GBCode < 1637) return "A";
if (GBCode >= 1637 && GBCode < 1833) return "B";
if (GBCode >= 1833 && GBCode < 2078) return "C";
if (GBCode >= 2078 && GBCode < 2274) return "D";
if (GBCode >= 2274 && GBCode < 2302) return "E";
if (GBCode >= 2302 && GBCode < 2433) return "F";
if (GBCode >= 2433 && GBCode < 2594) return "G";
if (GBCode >= 2594 && GBCode < 2787) return "H";
if (GBCode >= 2787 && GBCode < 3106) return "J";
if (GBCode >= 3106 && GBCode < 3212) return "K";
if (GBCode >= 3212 && GBCode < 3472) return "L";
if (GBCode >= 3472 && GBCode < 3635) return "M";
if (GBCode >= 3635 && GBCode < 3722) return "N";
if (GBCode >= 3722 && GBCode < 3730) return "O";
if (GBCode >= 3730 && GBCode < 3858) return "P";
if (GBCode >= 3858 && GBCode < 4027) return "Q";
if (GBCode >= 4027 && GBCode < 4086) return "R";
if (GBCode >= 4086 && GBCode < 4390) return "S";
if (GBCode >= 4390 && GBCode < 4558) return "T";
if (GBCode >= 4558 && GBCode < 4684) return "W";
if (GBCode >= 4684 && GBCode < 4925) return "X";
if (GBCode >= 4925 && GBCode < 5249) return "Y";
if (GBCode >= 5249 && GBCode <= 5589) return "Z";
return "";
}
}
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(0, 1);
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 > 5589) return "";
if (GBCode >= 1601 && GBCode < 1637) return "A";
if (GBCode >= 1637 && GBCode < 1833) return "B";
if (GBCode >= 1833 && GBCode < 2078) return "C";
if (GBCode >= 2078 && GBCode < 2274) return "D";
if (GBCode >= 2274 && GBCode < 2302) return "E";
if (GBCode >= 2302 && GBCode < 2433) return "F";
if (GBCode >= 2433 && GBCode < 2594) return "G";
if (GBCode >= 2594 && GBCode < 2787) return "H";
if (GBCode >= 2787 && GBCode < 3106) return "J";
if (GBCode >= 3106 && GBCode < 3212) return "K";
if (GBCode >= 3212 && GBCode < 3472) return "L";
if (GBCode >= 3472 && GBCode < 3635) return "M";
if (GBCode >= 3635 && GBCode < 3722) return "N";
if (GBCode >= 3722 && GBCode < 3730) return "O";
if (GBCode >= 3730 && GBCode < 3858) return "P";
if (GBCode >= 3858 && GBCode < 4027) return "Q";
if (GBCode >= 4027 && GBCode < 4086) return "R";
if (GBCode >= 4086 && GBCode < 4390) return "S";
if (GBCode >= 4390 && GBCode < 4558) return "T";
if (GBCode >= 4558 && GBCode < 4684) return "W";
if (GBCode >= 4684 && GBCode < 4925) return "X";
if (GBCode >= 4925 && GBCode < 5249) return "Y";
if (GBCode >= 5249 && GBCode <= 5589) return "Z";
return "";
}
}
- .NET中如何取得汉字或者拼音首字母(附源代码)
- 取得汉字拼音首字母 (asp.net C#)
- 取得汉字拼音首字母
- C# 中取得汉字拼音首字母
- 取得汉字的拼音首字母
- Excel 取得汉字拼音首字母
- 取得汉字拼音首字母函数,取得汉字串首字母
- c#取得汉字的拼音的首字母。 (转)
- c#取得汉字的拼音的首字母。
- C#取得汉字的拼音的首字母。
- c#取得汉字的拼音的首字母
- C#取得汉字的拼音的首字母
- c#取得汉字的拼音的首字母
- C#取得汉字的拼音的首字母
- C#取得汉字的拼音的首字母
- c#取得汉字的拼音的首字母
- C#取得汉字的拼音的首字母
- C#取得汉字的拼音的首字母
- SQL数据库的打包部署安装
- SQL语句导入导出大全(转载)
- c#2.0新特性:(一)泛型(Generics)
- c#2.0新特性:(二)匿名方法(Anonymous Methods)
- c#2.0新特性:(三)迭代器(Iterators)
- .NET中如何取得汉字或者拼音首字母(附源代码)
- ASP.NET程序中常用的三十三种代码(转载)
- MODULE_DEVICE_TABLE
- Windows Server 2008常用优化设置
- 模拟一个按键值 给系统处理
- mysqldb
- Intent 的 隐式意图
- 现在世界的文明是建立在欧洲文明之上的
- Sicily.1036. Crypto Columns