Languages
[Edit]
EN

C# / .NET - check if string contains only numbers

0 points
Created by:
Theodora-Battle
528

In this article, we would like to show you how to check if string contains only numbers in C#.

Quick solution:

or:

 

Practical example

Edit

In this example, we use regular expression with specified pattern to check if string contains only numbers.

Output:

2. Using System.Linq

Edit

In this example, we use All() method from System.Linq namespace combined with Char.IsDigit() to check if string contains only numbers.

Output:

References

Edit
  1. Regex.IsMatch Method (System.Text.RegularExpressions) | Microsoft Docs
  2. Enumerable.All Method (System.Linq) | Microsoft Docs
  3. Char.IsDigit Method (System) | Microsoft Docs
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join