Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Iam trying to convert string to integer. i couldnt convert. It throws error as
Input string was not in a correct format.



string snewidstring = "462414720-60TX6E0088";
     int snewid = Convert.ToInt32(snewidstring);




How to convert this string to integer.

What I have tried:

<pre>string snewidstring = "462414720-60TX6E0088";
     int snewid = Convert.ToInt32(snewidstring);
Posted
Updated 27-Jul-18 3:55am
Comments
Bryian Tan 25-Jul-18 23:57pm    
Just curious, what is the expected result?
Dave Kreskowiak 26-Jul-18 0:16am    
What did you expect was going to happen? That string has a hyphen and alphabetic letters in it. There's no way that's going to convert to an integer value.

You're going to have to define, in precise terms, what you expect to get out of that string and what you expect to be able to convert to an integer.
#realJSOP 27-Jul-18 11:12am    
He was on an adventure of discovery.
Patrice T 26-Jul-18 1:53am    
What integer did you expect for this string ?
#realJSOP 27-Jul-18 11:12am    
"Oranges".

 
Share this answer
 
Comments
#realJSOP 27-Jul-18 11:10am    
This is the answer. It's obvious that the string he's trying to convert is NOT a valid number. One of the integer TryParse() methods is the right way to go. Whoever voted you a "1" is a retard, and should reconsider that maybe their best possible vocation is sweeping standing water off sidewalks, or watching the machine that puts the ridges on the edges of checkers.
string snewidstring = "462414720-60TX6E0088";
                   int storein;
                   int.TryParse(snewidstring, out storein);
 
Share this answer
 
Comments
F-ES Sitecore 26-Jul-18 5:54am    
You can improve the efficiency of that code without change what it does by making this slight change;

string snewidstring = "462414720-60TX6E0088";
int storein = 0;
Well, it seems you at least tried... You're going to have to remove the - and TX and E which will give you a large number, so you'll have to go Int64.

This is how your string will convert.
string snewidstring = "4624147206060088";
     Int64 snewid = Convert.ToInt64(snewidstring);
 
Share this answer
 
Comments
#realJSOP 27-Jul-18 11:25am    
That code will throw exactly the same exception.
Dylvh 31-Jul-18 10:31am    
Have you tried the code to see if it throws the exact same error? ;-) It actually does convert without throwing an exception. Anyway, I agree that the tryparse is the correct answer.
#realJSOP 31-Jul-18 11:55am    
It will throw the same exception with the OP's example value. You're converting an actual integer string to an int64, so it will convert without an exception. Your answer is therefore incorrect, and my observation stands.
Dylvh 1-Aug-18 3:29am    
I understand. Much appreciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900