Checking whether a string is numeric is a common task in C# programming, especially when dealing with user input, file parsing, or data validation. In this article, we’ll explore five different methods to check if a string is numeric in c#, along with their pros and cons.
Why check if a string is Numeric?
before processing user input (e.g., from forms, APIs, or files) you must ensure it’s a valid number to avoid runtime errors like FormatException. Common scenarios include:
- Validating user input in a textbox
- parsing CSV or JSON data
- Converting strings to integers, floats, or decimals
Method 1: Using int.TryParse() (For Integers)
The TrypParse() method is the simplest way to check if a string is numeric in c#. This method takes in a string and tries to parse it as an integer. If the string is a valid integer, then the method will return true and save the parsed integer value in the out parameter
string input = "125690";
bool isNumeric = int.TryParse(input, out int number);
Console.WriteLine(isNumeric); // Output : True
Pros:
- Fast and efficient for integers
- Returns the parsed value if successful
Cons:
- Only works for integers (int), not floating-point numbers
Method 2: Using double.TryParse() (For Floating-Point Number)
if you need to check for decimal numbers, use double.TryParse().
string stringvalue = "9012.87";
bool isNumeric = int.TryParse(stringvalue , out double number);
Console.WriteLine(isNumeric); // Output : True
Pros:
- Handles both integers and floating-point numbers
- Supports scientific notation (e.g., “1.23e4”)
Cons:
- incorrectly parse non-numeric strings with symbols (e.g, “$123” fails)
Method 3: Using decimal.TryParse() (For Financial/Precise Calculations)
if you need to check for financial applications, decimal.TryParse() is more precise.
string stringvalue = "901.876";
bool isNumeric = decimal.TryParse(stringvalue , out decimal number);
Console.WriteLine(isNumeric); // Output : True
Pros:
- Better precision for financial calculations
- Avoids floating-point rounding errors
Cons:
- Slightly slower than double.TryParse()
Methos 4: Using Regex (For Custom Number Formats)
if you need strict control over number formats use regular expressions.
using System.Text.RegularExpressions;
string stringvalue = "-9012.87";
bool isNumeric = Regex.IsMatch(stringvalue, @"^-?\d+(\.\d+)?$");
Console.WriteLine(isNumeric); // Output : True
Pros:
- Customizable for specific formats
- It works negative number
Cons:
- Slower than TryParse methods
- Complex regex can be hard to maintain
Methos 5: Using char.IsDigit() (For Simple Digit Checks)
if you only need to check if every character is a digit(no decimals or signs), use char.IsDigit().
string stringvalue = "901287";
bool isNumeric = strignvalue.All(char.IsDigit);
Console.WriteLine(isNumeric); // Output : True
Pros:
- Simple for pure digit strings
Cons:
- It fails to support negative numbers or decimals
Performance Comparison
Method | Best For | Speed |
int.TryParse() | integers | Fastest |
double.Tryparse() | Floating-point numbers | Fast |
decimal.TryParse() | Financial precision | Fast |
Regex | custom formats | Slower |
chr.IsDigit() | Pure digit only | Fast |