SoFunction
Updated on 2025-03-08

Example of c# conversion method of full-width half-width


using System;
using ;
using ;

namespace quanbanjiao
{
    public class Class1
    {
        /**/
        /// <summary>
/// Determine whether a character is in English half-width characters or punctuation
        /// </summary>
        /// <remarks>
/// 32    Space
/// 33-47    Punctuation
        /// 48-57    0~9
/// 58-64    Punctuation
        /// 65-90    A~Z
/// 91-96     Punctuation
        /// 97-122    a~z
/// 123-126  Punctuation
        /// </remarks>
        public static bool IsBjChar(char c)
        {
            int i = (int)c;
            return i >= 32 && i <= 126;
        }

        /**/
        /// <summary>
/// Determine whether a character is full-width character or punctuation
        /// </summary>
        /// <remarks>
/// <para>Full-width characters - 65248 = Half-width characters</para>
/// <para>Full-width space exception</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>
/// Convert full-width characters in a string to half-width
        /// </summary>
        public static string ToBj(string type,string s)
        {
            if (s == null || () == ) return s;

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

//If it is of int type, you can only enter int type, otherwise it will be automatically set to 0.

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

            }

 

//If it is float or double type, you can only enter these two types, otherwise it will be automatically set to 0.
            if (() == "float" || () == "double")
            {
                try
                {
                    (());
                }
                catch
                {
                    return "0";
                }
            }
            return ();
        }
    }
}