dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.26k stars 5.89k forks source link

C# Convert String Error #20781

Open harbaby28 opened 4 years ago

harbaby28 commented 4 years ago

dsSAACAS

System.FormatException: 'Input string was not in a correct format.' SEND HELP :(

Youssef1313 commented 4 years ago

@harbaby28 Are you following a tutorial/article from docs.microsoft.com?

In case you're following a tutorial/article, please link to it.

To answer the question, the value you have for str variable is "abcde". You're trying to split it by space, that will give an array containing "abcde" as the only element. After that, the call of int.Parse on this single element will generate an exception. You can't parse "abcde" as an integer.

The correction to the code is probably based on how you want things to work. But generally, you'll need to call TryParse on each array element after you split it, then take an appropriate action on whether TryParse returned true (success) or false (failure).

SeanKilleen commented 2 years ago

@BillWagner pinging you as I look at some older issues. I think this request has been answered accurately and can thus be closed.