30

I am looking for a function that can check the character if it is a integer and do something is so.

char a = '1';

if (Function(a))
{
  do something
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
darking050
  • 617
  • 1
  • 7
  • 20
  • Side note: you should consider if you need "interger" (as sequence of digits - there are couple characters like this), "digit" (0-9 in multiple languages) or "a number" (1/2,...) – Alexei Levenkov Oct 12 '12 at 20:06

9 Answers9

39

Use System.Char.IsDigit method

  • 1
    +1 to both IsDigit and IsNumber. Note that neither guarantees that character is "integer" because IsDigit simply means it is on digit of potentially longer integer, IsNumber could be float (i.e. 1/2). – Alexei Levenkov Oct 12 '12 at 20:04
  • 1
    IsDigit covers 0-9 and equivalents in other character sets, and is always an integer for a single character (for a longer string, use Integer.TryParse). IsNumber returns true for 0-9 as well as for some more interesting Unicode characters in the "Number, Other" and "Number, Letter" groups like ½ (that's 1 character) http://www.fileformat.info/info/unicode/category/No/list.htm – lgaud Oct 12 '12 at 20:46
  • IsDigit will return true for all of these characters; other than 0-9 they won't be parsed as Integers through int.TryParse with default culture settings on an English machine - http://www.fileformat.info/info/unicode/category/Nd/list.htm – lgaud Oct 12 '12 at 21:04
25

If you want just the pure 0-9 digits, use

if(a>='0' && a<='9')

IsNumeric and IsDigit both return true for some characters outside the 0-9 range:

Difference between Char.IsDigit() and Char.IsNumber() in C#

Community
  • 1
  • 1
D Stanley
  • 149,601
  • 11
  • 178
  • 240
6

Integer.TryParse works well.

http://msdn.microsoft.com/en-us/library/f02979c7.aspx

lhan
  • 4,585
  • 11
  • 60
  • 105
  • 1
    IsNumeric returns `true` for non-digit characters: http://stackoverflow.com/questions/228532/difference-between-char-isdigit-and-char-isnumber-in-c-sharp – D Stanley Oct 12 '12 at 20:01
  • Thanks for the heads up about IsNumber, I wasn't aware of that. I'll remove that from my answer. Do you know if Integer.TryParse does the same thing for non-digit characters? – lhan Oct 12 '12 at 20:03
  • Never tried it, but with a US culture I suspect it would just consider `0-9`. – D Stanley Oct 12 '12 at 20:05
6

The bool Char.IsDigit(char c); Method should work perfectly for this instance.

char a = '1';

if (Char.IsDigit(a))
{
  //do something
}
A.Clymer
  • 472
  • 3
  • 9
2

Try using System.Char.IsDigit method.

Raab
  • 34,778
  • 4
  • 50
  • 65
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Try Char.IsNumber. Documentation and examples can be found here

Aamir
  • 5,324
  • 2
  • 30
  • 47
0

It may be better to just use a switch statement. Something like:

switch(a)
{
  case '1':
    //do something.
    break;
  case '2':
    // do something else.
    break;
  default: // Not an integer
    throw new FormatException();
    break;
}

This will work as long as you're only looking for characters 0-9. Anything more than that (say "10") would be a string and not a character. If you're trying to just see if some input is an integer and the input is a string, you can do:

try
{
  Convert.ToInt32("10")
}
catch (FormatException err)
{
  // Not an integer, display some error.
}
Corith Malin
  • 1,505
  • 10
  • 18
0

I have to check the first to characters of a string and if the third character is numeric and do it with MyString.All(char.IsDigit):

if (cAdresse.Trim().ToUpper().Substring(0, 2) == "FZ" & cAdresse.Trim().ToUpper().Substring(2, 1).All(char.IsDigit))
FredyWenger
  • 2,236
  • 2
  • 32
  • 36
-1

Simplest answer:

char chr = '1';
char.isDigit(chr)
Ctrl S
  • 1,053
  • 12
  • 31
Lê Vũ Huy
  • 714
  • 6
  • 16