regex - How to convert a string with comma to integer in C# -


for example have string "99,999 abc xyz" want convert integer "99999"

i have made code worked:

 var regex = new regex("[a-z]");  linksonpage = regex.replace(linksonpage, "");   regex = new regex(" ");  linksonpage = regex.replace(linksonpage, "");   int nopage = int.parse(regex.replace(linksonpage, ""),                          numberstyles.allowthousands); 

but feel it's not enough, can me make shorter?

this regex remove letters , spaces:

var regex = new regex(" |[a-z]"); linksonpage = regex.replace(linksonpage, ""); 

you use int.parse , add numberstyles.allowthousands flag:

int num = int.parse(linksonpage , numberstyles.allowthousands); 

or int.tryparse letting know if operation succeeded:

int num; if (int.tryparse(linksonpage , numberstyles.allowthousands,                  cultureinfo.invariantculture, out num)) {     // parse successful, use 'num' } 

or can try this:

int num = int.parse(linksonpage.replace(",", "")); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -