如何在 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
JavaScript
PHP