Use regular expression to check if a string is an Unsigned Integer in CSharp

Description

The following code shows how to use regular expression to check if a string is an Unsigned Integer.

Example


using System;/*from   w  ww .  j  av a  2 s . c  o  m*/
using System.Data;
using System.Text.RegularExpressions;

class Class1{
        static void Main(string[] args){
      string IsNotNum = "111west";
      string IsNum = "  +111  ";
      string IsFloat = "  23.11  ";
      string IsExp = "  +23 e+11  ";
      
      Console.WriteLine(IsUnsignedIntegerRegEx(IsNum));    // True
      Console.WriteLine(IsUnsignedIntegerRegEx(IsNotNum));  // False
      Console.WriteLine(IsUnsignedIntegerRegEx(IsFloat));    // False
      Console.WriteLine(IsUnsignedIntegerRegEx(IsExp));    // False
        }
    public static bool IsUnsignedIntegerRegEx(string str)
    {
      str = str.Trim();
      return (Regex.IsMatch(str, @"^\+?\d+$"));
    }
    
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var