在本教程中,我们将看到将Java中的String转换为int(或Integer)的各种方法。
您可以使用以下任何一种方式:
–使用Integer.parseInt(string)
–使用Integer.valueof(string)
–使用Apache Commons NumberUtils.toInt(string)
–使用Apache Commons NumberUtils.createInteger(string)
–使用Guava库的Ints.tryParse(string)方法
–使用Integer.decode(string)
–使用新的整数(字符串)

使用Integer.parseInt(string)
String empId1 = "1001"; int intEmpId1 = Integer.parseInt(empId1); System.out.println(intEmpId1); Output : 1001
在以下情况下,Integer.parseInt()将引发NumberFormatException:
/ Alphabets in the input. Integer.parseInt("100AB"); //Input number is greater than the Integer range. Integer.parseInt(""); //Number with decimal Integer.parseInt("1.1"); //empty String Integer.parseInt(""); //Blank space Integer.parseInt(" ");
使用Integer.valueof(string)
String empId2 = "2001"; Integer integerEmpId2 = Integer.valueOf(empId2); System.out.println(integerEmpId2); Output : 2001
使用Apache Commons NumberUtils.toInt(string)
String empId3 = "3001"; int intEmpId3 = NumberUtils.toInt(empId3); System.out.println(intEmpId3); Output : 3001 int intEmpId4 = NumberUtils.toInt(null); System.out.println(intEmpId4); Output : 0 int intEmpId5 = NumberUtils.toInt("1001ABC"); System.out.println(intEmpId5); Output : 0 int intEmpId6 = NumberUtils.toInt("1001ABC", 10); System.out.println(intEmpId6); Output : 10
使用Apache Commons NumberUtils.createInteger(string)
String empId4 = "4001"; Integer integerEmpId7 = NumberUtils.createInteger(empId4); System.out.println(integerEmpId7); Output : 4001
使用Guava库的Ints.tryParse(string)方法
String empId5 = "5001"; Integer integerEmpId8 = Ints.tryParse(empId5); System.out.println(integerEmpId8); Output : 5001
使用Integer.decode(string)
String empId6 = "6001"; Integer integerEmpId9 = Integer.decode(empId6); System.out.println(integerEmpId9); Output : 6001
使用新的整数(字符串)
String empId7 = "7001"; Integer integerEmpId10 = new Integer(empId7); System.out.println(integerEmpId10); Output : 7001
但是,请记住,从Java9开始不推荐使用此Integer构造函数。
完成程序
package com.blogspot.javasolutionsguide.stringtointexample; import org.apache.commons.lang3.math.NumberUtils; import com.google.common.primitives.Ints; public class StringToInt { public static void main(String[] args) { String empId1 = "1001"; int intEmpId1 = Integer.parseInt(empId1); System.out.println(intEmpId1); String empId2 = "2001"; Integer integerEmpId2 = Integer.valueOf(empId2); System.out.println(integerEmpId2); String empId3 = "3001"; int intEmpId3 = NumberUtils.toInt(empId3); System.out.println(intEmpId3); int intEmpId4 = NumberUtils.toInt(null); System.out.println(intEmpId4); int intEmpId5 = NumberUtils.toInt("1001ABC"); System.out.println(intEmpId5); int intEmpId6 = NumberUtils.toInt("1001ABC", 10); System.out.println(intEmpId6); String empId4 = "4001"; Integer integerEmpId7 = NumberUtils.createInteger(empId4); System.out.println(integerEmpId7); String empId5 = "5001"; Integer integerEmpId8 = Ints.tryParse(empId5); System.out.println(integerEmpId8); String empId6 = "6001"; Integer integerEmpId9 = Integer.decode(empId6); System.out.println(integerEmpId9); String empId7 = "7001"; Integer integerEmpId10 = new Integer(empId7); System.out.println(integerEmpId10); // Alphabets in the input. Integer.parseInt("100AB"); //Input number is greater than the Integer range. Integer.parseInt(""); //Number with decimal Integer.parseInt("1.1"); //empty String Integer.parseInt(""); //Blank space Integer.parseInt(" "); } }
使用的依赖项:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>16.0.1</version> </dependency>
摘要
翻译自: https://www.javacodegeeks.com/2020/03/how-to-convert-string-to-int-in-java.html
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233267.html原文链接:https://javaforall.net