How can I find which characters in a char[]
are numbers?
char[] example = { '2', 'a', '4', 'f', 'u', 'i', '6' };
if(example[3] == ???)
{
Console.WriteLine(example[3].toString());
}
How can I find which characters in a char[]
are numbers?
char[] example = { '2', 'a', '4', 'f', 'u', 'i', '6' };
if(example[3] == ???)
{
Console.WriteLine(example[3].toString());
}
So:
if (Char.IsDigit(example[3]))
{
Console.WriteLine(...);
}
If you want all the chars:
IEnumerable<char> digitList = example.Where(c => Char.IsDigit(c));
//or
char[] digitArray = example.Where(c => Char.IsDigit(c)).ToArray();
Use Char.IsNumber if you want all the extra "numbers" in Unicode, specifically:
Numbers include characters such as fractions, subscripts, superscripts, Roman numerals, currency numerators, encircled numbers, and script-specific digits.
Quite simply there is a method Char.IsNumber()
with which you can test:
char[] example = { '2', 'a', '4', 'f', 'u', 'i', '6' };
if(Char.IsNumber(example[3]))
{
Console.WriteLine(example[3].toString());
}
If you want to get all the numbers:
var numbers = example.Where(char.IsDigit);
If you want to check whether specific char is number or not:
if(char.IsDigit(example[3]))
static void Main(string[] args)
{
char[] example = { '2', 'a', '4', 'f', 'u', 'i', '6' };
if (char.IsDigit(example[3]))
{
Console.WriteLine(example[3]);
}
}