Given a string value and we have to convert it into an integer.
给定一个字符串值,我们必须将其转换为整数。
Java conversion from String to Integer
Java从String到Integer的转换
To convert a String to Integer, we can use the following methods of Integer class (see the syntax given below...)
要将String转换为Integer ,我们可以使用Integer类的以下方法(请参见下面给出的语法...)
Syntax:
句法:
Integer Integer.valueOf(String).intValue();
OR
Integer Integer.parseInt(String);
Java代码将字符串转换为整数 (Java code to convert a String to Integer)
//Java code to convert String to Integer
public class Main {
public static void main(String args[]) {
String str = "12345";
//variable to store result
int result = 0;
//converting string to integer
//method 1
result = Integer.valueOf(str).intValue();
System.out.println("result (value of str as integer) = " + result);
//method 2
result = Integer.parseInt(str);
System.out.println("result (value of str as integer) = " + result);
}
}
Output
输出量
result (value of str as integer) = 12345
result (value of str as integer) = 12345
翻译自: https://www.includehelp.com/java-programs/conversion-from-string-to-integer-in-java.aspx