cxi変換全角半角方法例


C〓の全角を半角に変換する
新しいプロジェクトquanbanjiaoを作成し、このプロジェクトの下でクラスクラスクラス1を新設します。

using System;
using System.Web;
using System.Text;

namespace quanbanjiao
{
    public class Class1
    {
        /**/
        /// <summary>
        ///
        /// </summary>
        /// <remarks>
        /// 32   
        /// 33-47   
        /// 48-57    0~9
        /// 58-64   
        /// 65-90    A~Z
        /// 91-96   
        /// 97-122    a~z
        /// 123-126 
        /// </remarks>
        public static bool IsBjChar(char c)
        {
            int i = (int)c;
            return i >= 32 && i <= 126;
        }

        /**/
        /// <summary>
        ///
        /// </summary>
        /// <remarks>
        /// <para> - 65248 = </para>
        /// <para> </para>
        /// </remarks>
        public static bool IsQjChar(char c)
        {
            if (c == '\u3000') return true;

            int i = (int)c - 65248;
            if (i < 32) return false;
            return IsBjChar((char)i);
        }

        /// <summary>
        ///
        /// </summary>
        public static string ToBj(string type,string s)
        {
            if (s == null || s.Trim() == string.Empty) return s;

            StringBuilder sb = new StringBuilder(s.Length);
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '\u3000')
                    sb.Append('\u0020');
                else if (IsQjChar(s[i]))
                    sb.Append((char)((int)s[i] - 65248));
                else
                    sb.Append(s[i]);
            }

            // int , int , 0

            if (type.ToLower() == "int")
            {
                try
                {
                    Convert.ToInt32(sb.ToString());
                }
                catch
                {
                    return "0";
                }

            }

 

            // float double , , 0
            if (type.ToLower() == "float" || type.ToLower() == "double")
            {
                try
                {
                    Convert.ToDouble(sb.ToString());
                }
                catch
                {
                    return "0";
                }
            }
            return sb.ToString();
        }
    }
}