C#全角、半角转换

时间:2024-08-29 17:06:14

  全角:指一个字符占用两个标准字符位置;半角:指一字符占用一个标准的字符位置。  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExtensionMethodsDemo
{
    static class Program
    {
        static void Main(string[] args)
        {
            string input = "深圳狐狼,你是好人!".ToSBC();
            string input2 = "chenyuming,you are a good man!".ToDBC();
            Console.WriteLine(input);
            Console.WriteLine(input2);
        }

        /// <summary>
        /// 转化为半角字符串(扩展方法)
        /// </summary>
        /// <param name="input">要转化的字符串</param>
        /// <returns>半角字符串</returns>
        public static string ToSBC(this string input)//single byte charactor
        {
            char[] c = input.ToCharArray();
            ; i < c.Length; i++)
            {
                )//全角空格为12288,半角空格为32
                {
                    c[i] = (;
                    continue;
                }
                 && c[i] < )//其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
                    c[i] = ();
            }
            return new string(c);
        }

        /// <summary>
        /// 转化为全角字符串(扩展方法)
        /// </summary>
        /// <param name="input">要转化的字符串</param>
        /// <returns>全角字符串</returns>
        public static string ToDBC(this string input)//double byte charactor
        {
            char[] c = input.ToCharArray();
            ; i < c.Length; i++)
            {
                )
                {
                    c[i] = (;
                    continue;
                }
                )
                    c[i] = ();
            }
            return new string(c);
        }
    }  

}

  因为扩展方法必须在静态类下定义,所以最好写成下面形式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExtensionMethodsDemo
{
    class Program //此类可不为静态类
    {
        static void Main(string[] args)
        {
            string input = ExtensionMethods.ToSBC("深圳狐狼,你是好人!");
            string input2 = ExtensionMethods.ToDBC("chenyuming,you are a good man!");
            Console.WriteLine(input);
            Console.WriteLine(input2);
        }
    }

    static class ExtensionMethods
    {
        /// <summary>
        /// 转化为半角字符串
        /// </summary>
        /// <param name="input">要转化的字符串</param>
        /// <returns>半角字符串</returns>
        public static string ToSBC(this string input)//single byte charactor
        {
            char[] c = input.ToCharArray();
            ; i < c.Length; i++)
            {
                )//全角空格为12288,半角空格为32
                {
                    c[i] = (;
                    continue;
                }
                 && c[i] < )//其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
                    c[i] = ();
            }
            return new string(c);
        }

        /// <summary>
        /// 转化为全角字符串
        /// </summary>
        /// <param name="input">要转化的字符串</param>
        /// <returns>全角字符串</returns>
        public static string ToDBC(this string input)//double byte charactor
        {
            char[] c = input.ToCharArray();
            ; i < c.Length; i++)
            {
                )
                {
                    c[i] = (;
                    continue;
                }
                )
                    c[i] = ();
            }
            return new string(c);
        }
    }
}

  运行结果:

C#全角、半角转换