//增加了小数与正负数的判断
//输入类型检查器
public class ValueChecker
{
    private string type;               
 //string,empty,int,string_number,negative_int,negative_decimal,decimal
    private int o_length = 0;           //原始字符数
    private int digicount = 0;          //数字个数
    private bool is_negative = false;   //是否负的,默认否
    private bool is_decimal = false;    //是否小数,默认否

    //取类型
    public string valueType
    {
        get { return type; }
        set { type = value; }
    }

    public int valueLength
    {
        get { return o_length; }
        set { o_length = value; }
    }

    public int valueCount
    {
        get { return digicount; }
        set { digicount = value; }
    }

    public bool valueIsNegative
    {
        get { return is_negative; }
        set { is_negative = value; }
    }

    public bool valueIsDecimal
    {
        get { return is_decimal; }
        set { is_decimal = value; }
    }

    //重置所有字段
    public void Clear(bool all)
    {
        if (all == true)
        {
            type = "";
            o_length = 0;
            digicount = 0;
            is_decimal = false;
            is_negative = false;
        }
    }

   
    public void CheckType(string T)
    {
         T = T.Trim();
         type = "";
         o_length = T.Length;

         //对正负数的判读:
         //检查是否仅有一个负号并且处于第一个位置,-0无效(0不可以是继负号后的最后一个字符)
         if (T.StartsWith("-") == true)
         {
             string try_t = T.Remove(0, 1);
             if (try_t != "0")
             {
                 T = T.Remove(0, 1);    //去掉负号,方便后期处理
                 is_negative = true;
             }
         }
       
        //对小数的判读:
        //检查小数点是唯一并且不处于第一个和最后一个位置
         if ( T.StartsWith(".") != true && T.IndexOf(".") > 0 && T.EndsWith(".") != true)
         {
             if (T.IndexOf("0",T.IndexOf(".") – 1) > -1)
             {
                 T = T.Remove(T.IndexOf(".") – 1, 1);    //如果小数点前第一个字符是0则去掉,方便后期处理 
             }
             T = T.Remove(T.IndexOf("."), 1);   //去掉小数点,方便后期处理
             is_decimal = true;
            
         }

        int length = T.Length; //字符数
       
        if (o_length > 0) //输入值不为空时
        {
            //检测是否含有数字,通过Unicode码识别
            //字符串转为数组
            Char[] TT = T.ToCharArray();
            digicount = length;     //假设所有字符为数字,数字个数为字符数
            foreach (char k in TT)
            {
                if ((short)k > 57 || (short)k < 48) //如果有一个字符不是数字,那数字个数减1
                {
                    //type += k + " >> Short(" + (short)k + ");";
                    digicount -= 1;
                }
            }

            //如果字符数大于 0 且所有字符都是数字,检测是数串(如:001),还是数值(如:100)
            if (length > 0 && digicount == length)
            {
                if (length > 1 && T.IndexOf("0", 0) != 0)   //如果大于1位字符(去符号后)并且首字符不以0开头就认定是数值
                {
                    if (is_decimal == false && is_negative == false)
                    {
                        type += "int";//正整数
                    }

                    if (is_decimal == false && is_negative == true)
                    {
                        type += "negative_int";//负整数
                    }

                    if (is_decimal == true && is_negative == false)
                    {
                        type += "decimal";//正小数
                    }

                    if (is_decimal == true && is_negative == true)
                    {
                        type += "negative_decimal";//负小数
                    }
                }
                else
                {
                    if (length == 1)    //如果只有1位字符(去符号后)
                    {
                        if (is_decimal == false && is_negative == false)
                        {
                            type += "int";//正整数
                        }

                        if (is_decimal == false && is_negative == true)
                        {
                            type += "negative_int";//负整数
                        }

                        if (is_decimal == true && is_negative == false)
                        {
                            type += "decimal";//正小数
                        }

                        if (is_decimal == true && is_negative == true)
                        {
                            type += "negative_decimal";//负小数
                        }
                    }
                    else    //如果大于一位字符并且0是首字符 或者是 0.00n的情况
                    {

                        //有小数点的情况
                        if (is_decimal == true)
                        {
                            if (is_negative == true)
                            {
                                type += "negative_decimal";
                            }
                            else
                            {
                                type += "decimal";
                            }
                        }
                        //无小数点的情况
                        else
                        {
                            type += "string_number";
                            is_decimal = false;     //重置小数判断
                            is_negative = false;    //重置负数判断
                        }
                    }
                }
            }
            else    //至少有一个字符不是数字
            {
                type += "string";
                is_decimal = false;     //重置小数判断
                is_negative = false;    //重置负数判断
               
            }
        }
        else    // o_length 为 0 当前值为空
        {
            type += "empty";
        }
    }

}


评论

该日志第一篇评论

发表评论

评论也有版权!