当前位置:编程学习 > C#/ASP.NET >>

判断是否是数字类

[csharp]
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
 
namespace shop 

    public class IsNum 
    { 
        /// <summary>  
        /// 判断是否是数字  
        /// </summary>  
        /// <param name="strNumber">要判断的字符串</param>  
        /// <returns></returns>  
        public bool IsNumber(String strNumber) 
        { 
            Regex objNotNumberPattern = new Regex("[^0-9.-]"); 
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*"); 
            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*"); 
            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$"; 
            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$"; 
            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")"); 
 
            return !objNotNumberPattern.IsMatch(strNumber) && 
             !objTwoDotPattern.IsMatch(strNumber) && 
             !objTwoMinusPattern.IsMatch(strNumber) && 
             objNumberPattern.IsMatch(strNumber); 
        }         
    } 

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

namespace shop
{
    public class IsNum
    {
        /// <summary>
        /// 判断是否是数字
        /// </summary>
        /// <param name="strNumber">要判断的字符串</param>
        /// <returns></returns>
        public bool IsNumber(String strNumber)
        {
            Regex objNotNumberPattern = new Regex("[^0-9.-]");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");

            return !objNotNumberPattern.IsMatch(strNumber) &&
             !objTwoDotPattern.IsMatch(strNumber) &&
             !objTwoMinusPattern.IsMatch(strNumber) &&
             objNumberPattern.IsMatch(strNumber);
        }       
    }
}


 

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,