如何在 C# 中判断字符串是否是数字?


假设我们的字符串为 −

string str = "3456";

现在,检查输入字符串是否为数字 −

str.All(c => char.IsDigit(c))

如果字符串是数字,则上面返回 true,否则返回 false。

以下为完整代码 −

示例

 在线演示

using System;
using System.Linq;

namespace Demo {
   public class MyApplication {
      public static void Main(string[] args) {
         string str = "3456";
         // checking if string is a number or not
         Console.WriteLine(str.All(c => char.IsDigit(c)));
      }
   }
}

输出

True

更新时间:2020-04-02

251 次浏览

开启您的 职业生涯

通过完成该课程获得认证

开始使用
广告
© . All rights reserved.