获取字符串的首字母(包括中文),应用于按26个字母过滤和排序!

   公司的项目中要实现按26个字母过滤出相应的字段并且要区分多语言,一般来说拉丁字母很好,可是象中文这样的就不太好弄了,后来经过同事的帮忙在网上找了一些代码,能实现根据首字母过滤中文,现贴于下,做个记号.

核心处理类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace WebApplication1
{
    
public class GetAlphaFirst
    
{
        
public GetAlphaFirst()
        
{

        }


        
/**/
        
/// <summary>
        
/// 获得给定字符串的首字母
        
/// </summary>
        
/// <param name="IndexTxt">需要获得的字符串</param>
        
/// <returns></returns>

        public string GetIndexCode(string indexTxt)
        
{
            
return IndexCode(indexTxt);
        }

        
//返回给定字符串的首字母
        private String IndexCode(String IndexTxt)
        
{
            String _Temp 
= null;
            
for (int i = 0; i < IndexTxt.Length; i++)
                _Temp 
= _Temp + GetOneIndex(IndexTxt.Substring(i, 1));
            
return _Temp;
        }


        
//得到单个字符的首字母
        private String GetOneIndex(String OneIndexTxt)
        
{
            
if (Convert.ToChar(OneIndexTxt) >= 0 && Convert.ToChar(OneIndexTxt) < 256)
                
return OneIndexTxt;
            
else
            
{
                Encoding gb2312 
= Encoding.GetEncoding("gb2312");
                
byte[] unicodeBytes = Encoding.Unicode.GetBytes(OneIndexTxt);
                
byte[] gb2312Bytes = Encoding.Convert(Encoding.Unicode, gb2312, unicodeBytes);
                
return GetX(Convert.ToInt32(String.Format("{0:D2}", Convert.ToInt16(gb2312Bytes[0]) - 160+ String.Format("{0:D2}", Convert.ToInt16(gb2312Bytes[1]) - 160)));
            }


        }


        
//根据区位得到首字母
        private String GetX(int GBCode)
        
{
            
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";
            
if (GBCode >= 5601 && GBCode <= 8794)
            
{
                String CodeData 
= "cjwgnspgcenegypbtwxzdxykygtpjnmjqmbsgzscyjsyyfpggbzgydywjkgaljswkbjqhyjwpdzlsgmr"
                                  
+ "ybywwccgznkydgttngjeyekzydcjnmcylqlypyqbqrpzslwbdgkjfyxjwcltbncxjjjjcxdtqsqzycdxxhgckbphffss"
                                  
+ "pybgmxjbbyglbhlssmzmpjhsojnghdzcdklgjhsgqzhxqgkezzwymcscjnyetxadzpmdssmzjjqjyzcjjfwqjbdzbjgd"
                                  
+ "nzcbwhgxhqkmwfbpbqdtjjzkqhylcgxfptyjyyzpsjlfchmqshgmmxsxjpkdcmbbqbefsjwhwwgckpylqbgldlcctnma"
                                  
+ "eddksjngkcsgxlhzaybdbtsdkdylhgymylcxpycjndqjwxqxfyyfjlejbzrwccqhqcsbzkymgplbmcrqcflnymyqmsqt"
                                  
+ "rbcjthztqfrxchxmcjcjlxqgjmshzkbswxemdlckfsydsglycjjssjnqbjctyhbftdcyjdgwyghqfrxwckqkxebpdjpx"
                                  
+ "jqsrmebwgjlbjslyysmdxlclqkxlhtjrjjmbjhxhwywcbhtrxxglhjhfbmgykldyxzpplggpmtcbbajjzyljtyanjgbj"
                                  
+ "flqgdzyqcaxbkclecjsznslyzhlxlzcghbxzhznytdsbcjkdlzayffydlabbgqszkggldndnyskjshdlxxbcghxyggdj"
                                  
+ "mmzngmmccgwzszxsjbznmlzdthcqydbdllscddnlkjyhjsycjlkohqasdhnhcsgaehdaashtcplcpqybsdmpjlpcjaql"
                                  
+ "cdhjjasprchngjnlhlyyqyhwzpnccgwwmzffjqqqqxxaclbhkdjxdgmmydjxzllsygxgkjrywzwyclzmcsjzldbndcfc"
                                  
+ "xyhlschycjqppqagmnyxpfrkssbjlyxyjjglnscmhcwwmnzjjlhmhchsyppttxrycsxbyhcsmxjsxnbwgpxxtaybgajc"
                                  
+ "xlypdccwqocwkccsbnhcpdyznbcyytyckskybsqkkytqqxfcwchcwkelcqbsqyjqcclmthsywhmktlkjlychwheqjhtj"
                                  
+ "hppqpqscfymmcmgbmhglgsllysdllljpchmjhwljcyhzjxhdxjlhxrswlwzjcbxmhzqxsdzpmgfcsglsdymjshxpjxom"
                                  
+ "yqknmyblrthbcftpmgyxlchlhlzylxgsssscclsldclepbhshxyyfhbmgdfycnjqwlqhjjcywjztejjdhfblqxtqkwhd"
                                  
+ "chqxagtlxljxmsljhdzkzjecxjcjnmbbjcsfywkbjzghysdcpqyrsljpclpwxsdwejbjcbcnaytmgmbapclyqbclzxcb"
                                  
+ "nmsggfnzjjbzsfqyndxhpcqkzczwalsbccjxpozgwkybsgxfcfcdkhjbstlqfsgdslqwzkxtmhsbgzhjcrglyjbpmljs"
                                  
+ "xlcjqqhzmjczydjwbmjklddpmjegxyhylxhlqyqhkycwcjmyhxnatjhyccxzpcqlbzwwwtwbqcmlbmynjcccxbbsnzzl"
                                  
+ "jpljxyztzlgcldcklyrzzgqtgjhhgjljaxfgfjzslcfdqzlclgjdjcsnclljpjqdcclcjxmyzftsxgcgsbrzxjqqcczh"
                                  
+ "gyjdjqqlzxjyldlbcyamcstylbdjbyregklzdzhldszchznwczcllwjqjjjkdgjcolbbzppglghtgzcygezmycnqcycy"
                                  
+ "hbhgxkamtxyxnbskyzzgjzlqjdfcjxdygjqjjpmgwgjjjpkjsbgbmmcjssclpqpdxcdyykypcjddyygywchjrtgcnyql"
                                  
+ "dkljczzgzccjgdyksgpzmdlcphnjafyzdjcnmwescsglbtzcgmsdllyxqsxsbljsbbsgghfjlwpmzjnlyywdqshzxtyy"
                                  
+ "whmcyhywdbxbtlmswyyfsbjcbdxxlhjhfpsxzqhfzmqcztqcxzxrdkdjhnnyzqqfnqdmmgnydxmjgdhcdycbffallztd"
                                  
+ "ltfkmxqzdngeqdbdczjdxbzgsqqddjcmbkxffxmkdmcsychzcmljdjynhprsjmkmpcklgdbqtfzswtfgglyplljzhgjj"
                                  
+ "gypzltcsmcnbtjbhfkdhbyzgkpbbymtdlsxsbnpdkleycjnycdykzddhqgsdzsctarlltkzlgecllkjljjaqnbdggghf"
                                  
+ "jtzqjsecshalqfmmgjnlyjbbtmlycxdcjpldlpcqdhsycbzsckbzmsljflhrbjsnbrgjhxpdgdjybzgdlgcsezgxlblg"
                                  
+ "yxtwmabchecmwyjyzlljjshlgndjlslygkdzpzxjyyzlpcxszfgwyydlyhcljscmbjhblyjlycblydpdqysxktbytdkd"
                                  
+ "xjypcnrjmfdjgklccjbctbjddbblblcdqrppxjcglzcshltoljnmdddlngkaqakgjgyhheznmshrphqqjchgmfprxcjg"
                                  
+ "dychghlyrzqlcngjnzsqdkqjymszswlcfqjqxgbggxmdjwlmcrnfkkfsyyljbmqammmycctbshcptxxzzsmphfshmclm"
                                  
+ "ldjfyqxsdyjdjjzzhqpdszglssjbckbxyqzjsgpsxjzqznqtbdkwxjkhhgflbcsmdldgdzdblzkycqnncsybzbfglzzx"
                                  
+ "swmsccmqnjqsbdqsjtxxmbldxcclzshzcxrqjgjylxzfjphymzqqydfqjjlcznzjcdgzygcdxmzysctlkphtxhtlbjxj"
                                  
+ "lxscdqccbbqjfqzfsltjbtkqbsxjjljchczdbzjdczjccprnlqcgpfczlclcxzdmxmphgsgzgszzqjxlwtjpfsyaslcj"
                                  
+ "btckwcwmytcsjjljcqlwzmalbxyfbpnlschtgjwejjxxglljstgshjqlzfkcgnndszfdeqfhbsaqdgylbxmmygszldyd"
                                  
+ "jmjjrgbjgkgdhgkblgkbdmbylxwcxyttybkmrjjzxqjbhlmhmjjzmqasldcyxyqdlqcafywyxqhz";
                String _gbcode 
= GBCode.ToString();
                
int pos = (Convert.ToInt16(_gbcode.Substring(02)) - 56* 94 + Convert.ToInt16(_gbcode.Substring(_gbcode.Length - 22));
                
return CodeData.Substring(pos - 11);
            }

            
return "";
        }

    }

}


aspx页:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
     
<div>
        输入汉字
        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        
<br />
        
<br />
        拼音缩写
        
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
        
<br />
        
<asp:Button ID="Button1" runat="server" Text="获得拼音缩写" OnClick="Button1_Click" />
    
    
</div>
    
</form>
</body>
</html>

aspx.cs文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
    
public partial class _Default : System.Web.UI.Page
    
{
        
protected void Page_Load(object sender, EventArgs e)
        
{

        }

        
protected void Button1_Click(object sender, EventArgs e)
        
{
            
if (TextBox1.Text != "")
            
{
                GetAlphaFirst py 
= new GetAlphaFirst();
                Label1.Text 
= py.GetIndexCode(TextBox1.Text);
            }

            
else
            
{
                Label1.Text 
= "请输入要查询的文字";
            }

        }

    }

}

posted on 2007-06-17 23:27  不懂  阅读(948)  评论(0编辑  收藏  举报

导航