阅读35 返回首页    go 技术社区[云栖]


C#/asp.net中如何将数字全角半角互转

C#/asp.net中如何将数字全角半角互转?

    //用户公用静态类,包含公用静态方法
    /// <summary>
    /// 用户公用静态类,包含公用静态方法
    /// </summary>
    public static class UserPublicStaticMethod
    {

        //把字母,数字由半角转化为全角
        /// <summary>
        /// 把字母,数字由半角转化为全角
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <returns>全角字符串</returns>
        public static string ChangeStrToSBC(string str)
        {

            char[] c = str.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
                if (b.Length == 2)
                {
                    if (b[1] == 0)
                    {
                        b[0] = (byte)(b[0] - 32);
                        b[1] = 255;
                        c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
                    }
                }
            }
            //半角  
            string strNew = new string(c);
            return strNew;
        }

        //将字母,数字由全角转化为半角
        /// <summary>
        /// 将字母,数字由全角转化为半角
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <returns>半角字符串</returns>
        public static string ChangeStrToDBC(string str)
        {
            string s = str;
            char[] c = s.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
                if (b.Length == 2)
                {
                    if (b[1] == 255)
                    {
                        b[0] = (byte)(b[0] + 32);
                        b[1] = 0;
                        c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
                    }
                }
            }
            //半角  
            string news = new string(c);
            return news;
        }

}

最后更新:2017-04-02 06:51:42

  上一篇:go 手动布设陷阱链接
  下一篇:go VirtualBox 导出虚拟电脑可能遇到的陷阱