Determines whether the specified s item is integer with Regex. - CSharp System.Text.RegularExpressions

CSharp examples for System.Text.RegularExpressions:Match Number

Description

Determines whether the specified s item is integer with Regex.

Demo Code


using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Globalization;
using System;//from w  w  w .  j a  va 2 s  . c om

public class Main{
        /// <summary>
        /// Determines whether the specified s item is integer.
        /// </summary>
        /// <param name="sItem">The s item.</param>
        /// <returns>
        ///    <c>true</c> if the specified s item is integer; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsInteger(this string sItem)
        {
            Regex notIntPattern = new Regex("[^0-9-]");
            Regex intPattern = new Regex("^-[0-9]+$|^[0-9]+$");

            return !notIntPattern.IsMatch(sItem) && intPattern.IsMatch(sItem);
        }
}

Related Tutorials