How to search for number in string with method Contains and Linq in c#? -
i create calculator have buttons numbers , operators basic operation (+, -,...) , want filter buttons numbers detect when clicked number (between 0-9). put new eventhadler convert sender button.
i wondering nice , easy way filter buttons numbers (using linq) did try far is
if(btn.text == btn.text.contains(x => x >= '0' && x <= '9')) messagebox.show("number " + btn.text + " pressed!");
how make upper code workable?
if want know is number or not this. no linq required
linq way check numbers between 0 , 9
if(yourstring.tochararray().where(c=> c < '0' || c > '9').any()) return false; else return true;
to check contains valid number
double num; if (double.tryparse(btn.text, out num)) { // it's number! }
or check less 10 without linq
static bool islessthanten(string str) { foreach (char c in str) { if (c < '0' || c > '9') return false; } return true; }
Comments
Post a Comment