Home  >  Article  >  How to convert string to int

How to convert string to int

zbt
zbtOriginal
2023-08-02 10:54:078885browse

Methods to convert string to int: 1. Use built-in functions or methods; 2. Use numerical conversion method (string needs to be converted to numerical value first)

How to convert string to int

In programming, converting strings to integers is a common need. Fortunately, most programming languages ​​provide built-in functions or methods to achieve this functionality. Below are some common ways to convert strings to integers.

1. Use built-in functions or methods:

a. Python: The int() function can convert a string into an integer. For example:

num_str = "42"
num_int = int(num_str)
print(num_int) # 输出结果为 42

b. JavaScript: The parseInt() function can convert a string into an integer. For example:

var num_str = "42";
var num_int = parseInt(num_str);
console.log(num_int); // 输出结果为 42

c. Java: The Integer.parseInt() method can convert a string into an integer. For example:

String num_str = "42";
int num_int = Integer.parseInt(num_str);
System.out.println(num_int); // 输出结果为 42

d. C: The std::stoi() function can convert a string to an integer. For example:

#include
#include
int main() {
std::string num_str = "42";
int num_int = std::stoi(num_str);
std::cout << num_int << std::endl; // 输出结果为 42
return 0;
}

2. Use the numerical conversion method (need to convert the string to a numerical value first):

a. Python: Use the eval() function to convert the string to a numerical value, and then Convert numeric value to integer. For example:

num_str = "42"
num_int = int(eval(num_str))
print(num_int) # 输出结果为 42

b. JavaScript: Use the Number() function to convert a string to a numeric value, and then convert the numeric value to an integer. For example:

var num_str = "42";
var num_int = parseInt(Number(num_str));
console.log(num_int); // 输出结果为 42

c. Java: Use the Double.parseDouble() method to convert a string to a numeric value, and then convert the numeric value to an integer. For example:

String num_str = "42";
int num_int = (int) Double.parseDouble(num_str);
System.out.println(num_int); // 输出结果为 42

d. C: Use the std::stoi() function to convert the string to a numeric value, and then convert the numeric value to an integer. For example:

#include
#include
#include
int main() {
std::string num_str = "42";
std::istringstream iss(num_str);
int num_int;
iss >> num_int;
std::cout << num_int << std::endl; // 输出结果为 42
return 0;
}

It should be noted that if the string contains non-numeric characters, or the conversion result exceeds the range of integers, the above method may produce errors or abnormal results. In practical applications, the validity of the input string should be verified to ensure the accuracy and security of the conversion. .

The above is the detailed content of How to convert string to int. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn