2013-04-11 84 views
-2

的combinbation的字符串。調查具有完整的電話號碼或我要檢查字符串是否僅是數字,或者是字母數字和字符串

例如:

string test = "2323212343243423333"; 
string test1 = "34323df23233232323e"; 

我只想或不檢查有測試號。如果具有數字的整個字符串表示它返回true。否則它返回false。

我怎樣才能做到這一點?

+2

自己編寫函數'test1'是一個有效的十六進制數。 – leppie 2013-04-11 07:55:59

+5

看一看這樣的:http://stackoverflow.com/questions/273141/regex-for-numbers-only – 2013-04-11 07:56:04

+1

你用Google搜索**'檢查字符串是否僅numeric' **? – I4V 2013-04-11 07:59:33

回答

0

如果字符串長度不能太長,您可以嘗試int.TryParse(string here)或者您可以通過檢查字符串中的每一個字符像

if(MyString[i]-'0'<= 9 && MyString[i]-'0'>= 0) 
//then it's a digit, and check other characters this way 
+0

OP給出的例子肯定是太長了int.TryParse() – 2013-04-11 08:03:17

+0

是的,我已經說清楚了,我猜:) – 2013-04-11 08:04:41

3
bool allDigits = text.All(c => char.IsDigit(c)); 

或者

bool allDigits = text.All(char.IsDigit); 

除非 「數字」 你有十六進制數?我的答案只適用於只包含數字的字符串,當然。

相關問題