Friday, October 03, 2008

IsInteger Function in C#

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

1 comments:

Sherpes said...

thanks for posting this. Many coders that migrate from VB.NET to C# often include the libraries from VisualBasic.net so that they can use the functions IsNumeric, IsDate, etc etc.

This one is better solution...