简体   繁体   English

如何找到一个字符数组中的数字?

[英]How can I find the numbers in an array of characters?

How can I find which characters in a char[] are numbers? 如何找到char[]中的哪些字符是数字?

char[] example = { '2', 'a', '4', 'f', 'u', 'i', '6' };

if(example[3] == ???) 
{
  Console.WriteLine(example[3].toString());
}

char.IsDigit

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: 如果要使用Unicode中的所有其他“数字”,请使用Char.IsNumber,特别是:

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() 很简单就有一个方法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]);

            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM