Integer.parseInt(string s)方法
使用java Integer类的parseInt()方法进行转换。由于待转换的字符串中可能出现非数字的情况,需要捕获NumberFormatException的异常。
示例:
public class test1 {
public static void main(String[] args) {
String test_str = "123";
try {
int test_int = Integer.parseInt(test_str);
System.out.println(test_int);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}