Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string into an int

I'm having trouble converting a string into an integer. I googled it but all I can find is how to convert an int into a string. Does anyone know how to do it the other way around? Thanks.

like image 981
MacN00b Avatar asked Jul 09 '11 23:07

MacN00b


People also ask

Can you convert strings to ints?

In Python an strings can be converted into a integer using the built-in int() function. The int() function takes in any python data type and converts it into a integer.

Can you turn a string into an int in Python?

Strings can be converted to numbers by using the int() and float() methods. If your string does not have decimal places, you'll most likely want to convert it to an integer by using the int() method.

How do I convert a string to an int in C++?

One effective way to convert a string object into a numeral int is to use the stoi() function. This method is commonly used for newer versions of C++, with is being introduced with C++11. It takes as input a string value and returns as output the integer version of it.

How is a string converted to an int in java?

The method generally used to convert String to Integer in Java is parseInt() of String class.


2 Answers

See the NSString Class Reference.

NSString *string = @"5"; int value = [string intValue]; 
like image 200
Espresso Avatar answered Oct 13 '22 06:10

Espresso


How about

[@"7" intValue]; 

Additionally if you want an NSNumber you could do

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter numberFromString:@"7"]; 
like image 26
Paul.s Avatar answered Oct 13 '22 04:10

Paul.s