-2

I want to check whether a string is only numeric, or is alphanumeric.

For example :

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

I want to check test having number only or not. If the whole string having number means it returns true. Otherwise it returns false.

How can i do this?

RB.
  • 36,301
  • 12
  • 91
  • 131
user2176150
  • 303
  • 3
  • 12
  • 21

2 Answers2

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

Or

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

Unless by "numeric" you include hex numbers? My answer only works for strings that contain only digits, of course.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

if the string length is not too long, you may try int.TryParse(string here) or you may write the function yourself by checking every character in the string like

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