程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#獲得漢字拼音首字幕以及全拼(不支持繁體)

C#獲得漢字拼音首字幕以及全拼(不支持繁體)

編輯:C#入門知識

public class ChineseCode
    {
        protected string _CnTxt;
        protected string _EnTxt;

        ///
        /// 要獲取字母的漢字
        ///
        public string CnTxt
        {
            get { return _CnTxt; }
            set { _CnTxt = value; }
        }
        ///
        /// 漢字的首字母
        ///
        public string EnTxt
        {
            get { return _EnTxt; }
            set { _EnTxt = value; }
        }
        ///
        /// 構造方法
        ///
        public ChineseCode()
        {

        }
        ///
        /// 構造方法,獲取漢字首字母拼音
        ///
        ///
        public ChineseCode(string txt)
        {
            _CnTxt = txt;
            _EnTxt = IndexCode(_CnTxt);
        }
       
        /// <summary>
        /// 獲取漢字的首字母
        /// </summary>
        /// <param name="IndexTxt">漢字</param>
        /// <returns>漢字的首字母</returns>
        public String IndexCode(String IndexTxt)
        {
            String _Temp = null;
            for (int i = 0; i < IndexTxt.Length; i++)
                _Temp = _Temp + GetOneIndex(IndexTxt.Substring(i, 1));
            return _Temp;
        }

        /// <summary>
        /// 單個漢字
        /// </summary>
        /// <param name="OneIndexTxt">漢字</param>
        /// <returns>首拼</returns>
        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)
                 ));
            }
        }

        /// <summary>
        /// 根據區位得到首字母
        /// </summary>
        /// <param name="GBCode">區位</param>
        /// <returns>首字母</returns>
        private String GetX(int GBCode)
        {
            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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved