c# 字符串中全角和半角字符互转

 public class ConvertDBCAndSBC
    {

        /// <summary>半角转成全角   
        /// 半角空格32,全角空格12288   
        /// 其他字符半角33~126,其他字符全角65281~65374,相差65248   
        /// </summary>   
        /// <param name="input"></param>   
        /// <returns></returns>   
        public static string DBCToSBC(string input)
        {
            char[] cc = input.ToCharArray();
            for (int i = 0; i < cc.Length; i++)
            {
                if (cc[i] == 32)
                {
                    // 表示空格   
                    cc[i] = (char)12288;
                    continue;
                }
                if (cc[i] < 127 && cc[i] > 32)
                {
                    cc[i] = (char)(cc[i] + 65248);
                }
            }
            return new string(cc);
        }

        /// <summary>全角转半角   
        /// 半角空格32,全角空格12288   
        /// 其他字符半角33~126,其他字符全角65281~65374,相差65248   
        /// </summary>   
        /// <param name="input"></param>   
        /// <returns></returns>   
        public static string SBCToDBC(string input)
        {
            char[] cc = input.ToCharArray();
            for (int i = 0; i < cc.Length; i++)
            {
                if (cc[i] == 12288)
                {
                    // 表示空格   
                    cc[i] = (char)32;
                    continue;
                }
                if (cc[i] > 65280 && cc[i] < 65375)
                {
                    cc[i] = (char)(cc[i] - 65248);
                }

            }
            return new string(cc);
        }
    }  

 

posted @ 2017-09-05 11:22  潇潇与偕  阅读(1044)  评论(0编辑  收藏  举报