25

How would I parse an empty string? int.Parse(Textbox1.text) gives me an error:

Input string was not in a correct format.
System.FormatException: Input string was not in a correct format.

If the text is empty (Textbox1.text = ''), it throws this error. I understand this error but not sure how to correct this.

Ry-
  • 218,210
  • 55
  • 464
  • 476
user575219
  • 2,346
  • 15
  • 54
  • 105
  • 2
    you wouldn't, check if the string is empty before trying to parse it. if(!string.IsNullOrEmpty(Textbox1.Text)). Obviously this will still give you an error if the string is not a number so add some validation to the input – SCB Feb 21 '12 at 04:35

8 Answers8

40

If you're looking to default to 0 on an empty textbox (and throw an exception on poorly formatted input):

int i = string.IsNullOrEmpty(Textbox1.Text) ? 0 : int.Parse(Textbox1.Text);

If you're looking to default to 0 with any poorly formatted input:

int i;
if (!int.TryParse(Textbox1.Text, out i)) i = 0;
userx
  • 3,769
  • 1
  • 23
  • 33
  • 2
    the second code snippet suggests that TryParse will leave the previous value intact when returning false but it will zero it instead. better write `int i; if (!int.TryParse(Textbox1.Text, out i)) i = 0;` or someone thinks the following would work `int i = -1; int.TryParse(Textbox1.Text, out i);` – Firo Jan 26 '15 at 07:33
  • `out i` - it will be assigned to `0` if not parsed, so for zero check and assignment are extra. – Qwertiy May 15 '16 at 15:09
  • Personally, I prefer ```IsNullOrWhitespace``` for this. – Immersive Oct 21 '20 at 14:17
17

Well, what do you want the result to be? If you just want to validate input, use int.TryParse instead:

int result;

if (int.TryParse(Textbox1.Text, out result)) {
    // Valid input, do something with it.
} else {
    // Not a number, do something else with it.
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
7
if(!String.IsNullOrEmpty(Textbox1.text))
    var number = int.Parse(Textbox1.text);

Or even better:

int number;

int.TryParse(Textbox1.Text, out number);
Alex
  • 34,899
  • 5
  • 77
  • 90
5

If the input is a number or an empty string this will work. It will return zero if the string is empty or else it will return the actual number.

int.Parse("0"+Textbox1.Text)
Deepu Reghunath
  • 8,132
  • 2
  • 38
  • 47
5

Try this:

int number;
if (int.TryParse(TextBox1.Text, out number))
{
    //Some action if input string is correct
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
Victor Chekalin
  • 676
  • 1
  • 8
  • 15
4

You could also use an extension method like this:

public static int? ToNullableInt32(this string s)
{
    int i;
    if (Int32.TryParse(s, out i)) return i;
    return null;
}

Here's the reference: How to parse a string into a nullable int in C# (.NET 3.5)

Community
  • 1
  • 1
devinbost
  • 4,658
  • 2
  • 44
  • 57
1

I had the same problem with int.TryParse() erroring with negative numbers.

Here's what I used and it worked:

int.TryParse("-1",NumberStyles.Number, CultureInfo.InvariantCulture, out var num);

The NumberStyles enum can be OR'd together with '|', so you can pick and choose the format combinations. NumberStyles.Number covers about any number.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
-2

you can wrap it with simple try/catch...

Desolator
  • 22,411
  • 20
  • 73
  • 96
  • 2
    Terrible way to accomplish this. Always validate the input - don't plan for exceptions. – Ry- Feb 21 '12 at 04:37
  • This is a valid alternative but you should be clear that you only want `catch(System.FormatException)` – Conrad Frix Feb 21 '12 at 04:37
  • @Umer - what I meant is you don't need to think a lot just wrap the code with it and get on with your life... – Desolator Feb 21 '12 at 06:14
  • @RobinVanPersi agreed, if you can't find a dirty solution, you can't think a faster solution :) – Umer Feb 21 '12 at 06:17
  • @Ryan Don't plan for exceptions? Can you explain? Please. – johnny Sep 09 '16 at 00:13
  • 1
    @johnny: Better worded as "exceptions are for exceptional circumstances" or something. Anyway, the point being this: if you can check your input in a straightforward way before feeding it to stuff, do so. It reads better and helps avoid swallowing exceptions you may not have intended to. – Ry- Sep 09 '16 at 02:33