小迪的Blog
学习ing...(注明:该Blog中的任何信息都非原创,只是作为个人的阅读笔记)
using System.Text.RegularExpressions;

    
class Class1
    
{
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            
//
            
// TODO: 在此处添加代码以启动应用程序
            
//
            String strToTest;
            Class1 objValidate
=new Class1();

            Console.Write(
"Enter a String to Test for Alphabets:");
            strToTest
=Console.ReadLine();
            
if(objValidate.IsAlpha(strToTest))
            
{
                Console.WriteLine(
"{0} is Valid Alpha String",strToTest);
            }

            
else
            
{
                Console.WriteLine(
"{0} is not a Valid Alpha String",strToTest);
            }


        }


        
// Function to test for Positive Integers.(正整数)

        
public bool IsNaturalNumber(String strNumber)
        
{
            Regex objNotNaturalPattern
=new Regex("[^0-9]");
            Regex objNaturalPattern
=new Regex("0*[1-9][0-9]*");

            
return !objNotNaturalPattern.IsMatch(strNumber) &&
                objNaturalPattern.IsMatch(strNumber);
        }


        
// Function to test for Positive Integers with zero inclusive(非负整数)

        
public bool IsWholeNumber(String strNumber)
        
{
            Regex objNotWholePattern
=new Regex("[^0-9]");

            
return !objNotWholePattern.IsMatch(strNumber);
        }


        
// Function to Test for Integers both Positive & Negative(整数)

        
public bool IsInteger(String strNumber)
        
{
            Regex objNotIntPattern
=new Regex("[^0-9-]");
            Regex objIntPattern
=new Regex("^-[0-9]+$|^[0-9]+$");

            
return !objNotIntPattern.IsMatch(strNumber) &&
                objIntPattern.IsMatch(strNumber);
        }


        
// Function to Test for Positive Number both Integer & Real

        
public bool IsPositiveNumber(String strNumber)
        
{
            Regex objNotPositivePattern
=new Regex("[^0-9.]");
            Regex objPositivePattern
=new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
            Regex objTwoDotPattern
=new Regex("[0-9]*[.][0-9]*[.][0-9]*");

            
return !objNotPositivePattern.IsMatch(strNumber) &&
                objPositivePattern.IsMatch(strNumber) 
&&
                
!objTwoDotPattern.IsMatch(strNumber);
        }


        
// Function to test whether the string is valid number or not(数字)

        
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);
        }


        
// Function To test for Alphabets(字母). 

        
public bool IsAlpha(String strToCheck)
        
{
            Regex objAlphaPattern
=new Regex("[^a-zA-Z]");

            
return !objAlphaPattern.IsMatch(strToCheck);
        }


        
// Function to Check for AlphaNumeric(包括字母与数字的).

        
public bool IsAlphaNumeric(String strToCheck)
        
{
            Regex objAlphaNumericPattern
=new Regex("[^a-zA-Z0-9]");

            
return !objAlphaNumericPattern.IsMatch(strToCheck); 
        }


    }
posted on 2005-03-24 23:13  小迪  阅读(710)  评论(0编辑  收藏  举报