如何在 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 次瀏覽

開啟您的 職業生涯

透過完成該課程獲得認證

開始使用
廣告