공용 방법: 한자의 자모(대문자)를 얻다

#region ###          1
        public static string GetChineseFirstChar(string chineseStr)
        {
            StringBuilder sb = new StringBuilder();
            int length = chineseStr.Length;
            for (int i = 0; i < length; i++)
            {
                char chineseChar = chineseStr[i];
                sb.Append(GetPYChar(chineseChar));
            }
            return sb.ToString();
        }

        private static string GetPYChar(char c)
        {
            //int ascCode = Microsoft.VisualBasic.Strings.Asc(c);

            //int temp = 65536 + ascCode;
            int temp = 65536 + GetAsciiCode(c);
            //int temp = 65536 + Convert.ToInt16(c);
            if (temp >= 45217 && temp <= 45252)
            {
                return "A";
            }
            else if (temp >= 45253 && temp <= 45760)
            {
                return "B";
            }
            else if (temp >= 45761 && temp <= 46317)
            {
                return "C";
            }
            else if (temp >= 46318 && temp <= 46825)
            {
                return "D";
            }
            else if (temp >= 46826 && temp <= 47009)
            {
                return "E";
            }
            else if (temp >= 47010 && temp <= 47296)
            {
                return "F";
            }
            else if (temp >= 47297 && temp <= 47613)
            {
                return "G";
            }
            else if (temp >= 47614 && temp <= 48118)
            {
                return "H";
            }
            else if (temp >= 48119 && temp <= 49061)
            {
                return "J";
            }
            else if (temp >= 49062 && temp <= 49323)
            {
                return "K";
            }
            else if (temp >= 49324 && temp <= 49895)
            {
                return "L";
            }
            else if (temp >= 49896 && temp <= 50370)
            {
                return "M";
            }
            else if (temp >= 50371 && temp <= 50613)
            {
                return "N";
            }
            else if (temp >= 50614 && temp <= 50621)
            {
                return "O";
            }
            else if (temp >= 50622 && temp <= 50905)
            {
                return "P";
            }
            else if (temp >= 50906 && temp <= 51386)
            {
                return "Q";
            }
            else if (temp >= 51387 && temp <= 51445)
            {
                return "R";
            }
            else if (temp >= 51446 && temp <= 52217)
            {
                return "S";
            }
            else if (temp >= 52218 && temp <= 52697)
            {
                return "T";
            }
            else if (temp >= 52698 && temp <= 52979)
            {
                return "W";
            }
            else if (temp >= 52980 && temp <= 53688)
            {
                return "X";
            }
            else if (temp >= 53689 && temp <= 54480)
            {
                return "Y";
            }
            else if (temp >= 54481 && temp <= 62289)
            {
                return "Z";
            }
            else
            {
                return c.ToString();
            }
        }
        /// 
        ///      ascii 
        /// 
        private static int GetAsciiCode(char chr)
        {
            Encoding ecode = Encoding.GetEncoding("gb18030");
            byte[] codebytes = ecode.GetBytes(chr.ToString());
            if (IsTwoByteChar(chr))
            {
                //         256,    
                //       ,  65536
                return (int)codebytes[0] * 256 + (int)codebytes[1] - 65536;
            }
            else
            {
                return (int)codebytes[0];
            }
        }

        /// 
        ///         。
        /// 
        private static bool IsTwoByteChar(char chr)
        {
            string str = chr.ToString();
            //         
            Encoding ecode = Encoding.GetEncoding("gb18030");
            if (ecode.GetByteCount(str) == 2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

호출 방법:
            string ss = Common.GetChineseFirstChar('나 a*% 사랑해 중국');     //Wa*%ANZG

좋은 웹페이지 즐겨찾기