TryParse()
int result; if (int.TryParse(myString, out result)) { //ok, do something } else { //not an int }
int n; bool isNumeric = int.TryParse("123a", out n);
using System.Text.RegularExpressions; Regex regex = new Regex(@"^[0-9]+$"); private bool IsInteger(string str) { try { if (String.IsNullOrWhiteSpace(str)) { return false; } if(!regex.IsMatch(str)) { return false; } return true; } catch(Exception ex) { MessageBox.Show(ex.Message); } return false; }
int result; myString="123"; if (int.TryParse(myString, out result)) { Console.WriteLine("String is numeric"); } else { Console.WriteLine("String is non numeric"); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)