C# Testing to See If a String Is an Integer

Identify if a string is a number

int n;
bool isNumeric = int.TryParse("123", out n);

Update As of C# 7:

var isNumeric = int.TryParse("123", out int n);

or if you don't need the number you can discard the out parameter

var isNumeric = int.TryParse("123", out _);

The var s can be replaced by their respective types!

C# testing to see if a string is an integer?

Use the int.TryParse method.

string x = "42";
if(int.TryParse(x, out int value))
// Do something

If it successfully parses it will return true, and the out result will have its value as an integer.

In C#, how to check whether a string contains an integer?

The answer seems to be just no.

Although there are many good other answers, they either just hide the uglyness (which I did not ask for) or introduce new problems (edge cases).

How can I check if a string is a number?

Look up double.TryParse() if you're talking about numbers like 1, -2 and 3.14159. Some others are suggesting int.TryParse(), but that will fail on decimals.

string candidate = "3.14159";
if (double.TryParse(candidate, out var parsedNumber))
{
// parsedNumber is a valid number!
}

EDIT: As Lukasz points out below, we should be mindful of the thread culture when parsing numbers with a decimal separator, i.e. do this to be safe:

double.TryParse(candidate, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var parsedNumber)

Checking if a string contains an int

int.ParseInt will pass only when name is an int, and has no other characters.

You can check if a string contains a number anywhere in it with LINQ using Any:

if (name.Any(Char.IsDigit)) {
...
}

Check if the string is number is decimal or integer without using contains comma or dot

In your case, I would use NumberStyles, using NumberStyles.Integer might let your input be sure as an only number.

decimal.TryParse(value,NumberStyles.Integer,CultureInfo.CreateSpecificCulture("sv-SE"), out var eval)

c# fiddle

Checking whether string is empty, contains int or is an integer

With small changes to your code:

string name;

while (true)
{
Console.Write("Enter your name:");
name = Console.ReadLine();

bool isEmpty = string.IsNullOrEmpty(name);
bool isIntString = name.All(char.IsDigit);
bool containsInt = name.Any(char.IsDigit);
if (isEmpty)
{
Console.WriteLine("Name cannot be empty");
}
else if (isIntString)
{
Console.WriteLine("Your name cannot be made up of numbers");
}
else if (containsInt)
{
Console.WriteLine("Your name cannot contain numbers");
}
else
{
Console.WriteLine("Name filled");
break;
}
}

Console.WriteLine("Your name is: " + name);
Console.ReadKey();

With variable term:

static string GetUserInput(string term)
{
string name;

while (true)
{
Console.Write($"Enter your {term}:");
name = Console.ReadLine();

bool isEmpty = string.IsNullOrEmpty(name);
bool isIntString = name.All(char.IsDigit);
bool containsInt = name.Any(char.IsDigit);
if (isEmpty)
{
Console.WriteLine("Cannot be empty");
}
else if (isIntString)
{
Console.WriteLine("Cannot be made up of numbers");
}
else if (containsInt)
{
Console.WriteLine("Cannot contain numbers");
}
else
{
Console.WriteLine($"Your {term} filled");
break;
}
}

Console.WriteLine($"Your {term} is: {name}");
return name;
}


Related Topics



Leave a reply



Submit