Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to check numbers

Tags:

c#

linq

I have a string array. What is the simplest way to check if all the elements of the array are numbers

string[] str = new string[] { "23", "25", "Ho" };
like image 822
ScG Avatar asked Dec 14 '09 09:12

ScG


2 Answers

If you add a reference to the Microsoft.VisualBasic assembly, you can use the following one-liner:

bool isEverythingNumeric = 
    str.All(s => Microsoft.VisualBasic.Information.IsNumeric(s));
like image 86
Heinzi Avatar answered Oct 06 '22 15:10

Heinzi


You can do this:

var isOnlyNumbers = str.All(s =>
    {
        double i;
        return double.TryParse(s, out i);
    });
like image 22
Mark Seemann Avatar answered Oct 06 '22 15:10

Mark Seemann