Alex Rivera | Logout

int.Parse() with leading zeros

Asked 2009-11-05T00:00:18.213
19

How do I prevent the code below from throwing a FormatException. I'd like to be able to parse strings with a leading zero into ints. Is there a clean way to do this?

string value = "01";
int i = int.Parse(value);
Edit
Report

1 Answer

1

I have the following codes that may be helpful:

int i = int.Parse(value.Trim().Length > 1 ?
    value.TrimStart(new char[] {'0'}) : value.Trim());

This will trim off all extra leading 0s and avoid the case of only one 0.

answered 2011-12-17T17:24:04.690

Your Answer