Programming Course in C# ¡Free!

Function IsNumber

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

Create a function that tells if a string is an intenger number. It should be used like this:

if (IsNumber ("1234"))
System.Console.WriteLine ("It is a numerical value");

Output



Solution


using System;
public class FunctionIsNumeric
{
public static bool IsNumber(string text)
{
for (int i = 0; i < text.Length; i++)
if (text[i] < '0') || (text[i] > '9')
return false;

return true;
}


public static void Main()
{
Console.WriteLine( IsNumber("12") );
}
}