int.TryParse(判斷字符串是否爲數字組成)

int.TtyParse(string s, out int i)

用來判斷s字符串是否是由數字組成的,若是有數子組成,則將這個數字的值賦給i,同時這個式子返回bool類型的True。

若不是數字組成,則將i賦值爲0,並且返回值爲False。

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5.  
  6. namespace 個人練習 
  7.     class Program 
  8.     { 
  9.         static void Main(string[] args) 
  10.         { 
  11.             string s01 = "alex"
  12.             string s02 = "1234"
  13.             int i01; 
  14.             int i02; 
  15.             bool b01 = int.TryParse(s01, out i01); 
  16.             Console.WriteLine(s01 + " " + b01 + " " + i01); 
  17.             bool b02 = int.TryParse(s02, out i02); 
  18.             Console.WriteLine(s02 + " " + b02 + " " + i02); 
  19.         } 
  20.     } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章