There are so many programming languages in the world that are being used to create different applications. All of those programming languages, at some point, need to take data as input from the user and perform operations on user data. Some information is numeric while the other is alphabetic and some contain a combination of alphabets, numbers, and symbols. But everything entered by a user is in String format by default. So, in every programming language, there is a function that is used to convert that string to numbers. In this article, we are going to see the functions used to parse a string to integer in 5 different programming languages i.e. C, C++, Java, C#, and Python.
Parsing String to int in C:
To parse a string to int, we use the atoi() function. atoi() is the abbreviation of ASCII to Integer. The syntax for using this method is as follows:
atoi(string);
This method returns the integer representation of the string. If the string is not numeric, it returns 0. Here is an example program that demonstrates the use of the atoi() function:
#include
#include
#include
int main()
{
int number = atoi("6"); //Converting 6 from string to integer
printf("%d",number); //printing the result
return 0;
}
Output:
6
Parsing String to int in C++:
As you might already know that C++ is the advanced version of C, there is not a lot of difference in the functions used in C and C++. In C++, we have stoi() function. stoi is the abbreviated form of String to Integer. The syntax for using this function is as follows:
stoi(string);
This function returns the integer representation of the string passed to it. If the string is not numeric, it returns 0. Here is an example program:
#include
#include
using namespace std;
int main()
{
int number = stoi("6"); //Converting 6 from string to integer
cout
Output:
6
Parsing String to Int in Java:
Java is an easy-to-learn programming language that provides so many easy ways to perform complex tasks. There is an Integer class that contains a method named parseInt() that can be used to convert a string to an integer. Just like the rest functions in the rest of the programming languages, the parseInt() returns the numeric representation of the string. The syntax for using this method is as follows:
public class Main
{
public static void main(String[] args) {
String str = "1234";
int number = Integer.parseInt(str);
System.out.println(number);
}
}
Output:
1234
Parse String to Integer in C#:
In C#, we have two ways to convert a string to an integer:
- Using the ToInt() method of the Convert class.
- Using int.Parse() method
Let’s see both one by one:
Convert.ToInt():
This method is used according to the following syntax:
Convert.ToInt(string);
This method also returns the numeric representation of the string passed to it.
Example:
using System;
class HelloWorld {
static void Main() {
string str = "1234";
int number = Convert.ToInt32(str);
Console.WriteLine(number);
}
}
Output:
1234
Int32.Parse():
The syntax for using this method is as follows:
int32.Parse(string);
Example:
using System;
class HelloWorld {
static void Main() {
string str = "1234";
int number = Int32.Parse(str);
Console.WriteLine(number);
}
}
Output:
1234
Both of these methods throw an exception when the string passed to them is not numeric.
Parse String to Int in Python:
Python is the simplest yet complex language of all but converting a string to an integer is easiest in Python. You can do so by following the following syntax:
int(string)
Example:
print(int("1234"))
Output:
1234
If the string passed to this function is non-numeric, the Python gives an error.
I hope you find this article helpful!