如何在 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
廣告