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

后端 未结 10 1442
北恋
北恋 2020-11-28 12:00

I\'m just curious as to whether there is something built into either the C# language or the .NET Framework that tests to see if something is an integer

if (x         


        
10条回答
  •  误落风尘
    2020-11-28 12:24

    I think that I remember looking at a performance comparison between int.TryParse and int.Parse Regex and char.IsNumber and char.IsNumber was fastest. At any rate, whatever the performance, here's one more way to do it.

            bool isNumeric = true;
            foreach (char c in "12345")
            {
                if (!Char.IsNumber(c))
                {
                    isNumeric = false;
                    break;
                }
            }
    

提交回复
热议问题