如何在Java中将String转换为int

如何在Java中将String转换为int在本教程中 我们将看到将 Java 中的 String 转换为 int 或 Integer 的各种方法 您可以使用以下任何一种方式 使用 Integer parseInt string 使用 Integer valueof string 使用 ApacheCommon toInt string 使用 ApacheCommon

在本教程中,我们将看到将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)

–使用新的整数(字符串)

如何在Java中将String转换为int

使用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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 关于mysql的时区(下):如何设置mysql的时区

    关于mysql的时区(下):如何设置mysql的时区一、如何设置mysql时区1、命令1)查时区:showvariableslike’%time_zone%’返回有2行记录,要看time_zone变量的值,不需要看system_time_zone。若值为SYSTEM表示取值跟system_time_zone保持一致。system_time_zone的值是启动mysql服务的时候读取了操作系统的值,除非重新启动mysql服务重读否则这个值不变2)设置会话时区:settime_zone=’+8:00’仅对当前会话有效,在当前窗口立即生效

    2025年6月15日
    3
  • 蓝牙4.2对比蓝牙5.0_蓝牙 5.0 4.0区别

    蓝牙4.2对比蓝牙5.0_蓝牙 5.0 4.0区别目前市场上依然有大量蓝牙4.0/3.0/2.1/2.1+EDR产品存在,从自拍器,遥控器到各种智能设备,因其功能够用,价格低廉,受到快消类产品客户的亲昵,而工业类,汽车类应用,BT4.0的产品依然当道,究其原因,稳定,够用,供货好,当然价格不贵。但如果说蓝牙5之前蓝牙解决的是单点连接的可穿戴式设备与手机互联的问题,那么蓝牙5就是解决多点互联IoT物联网的问题。

    2025年10月10日
    5
  • Unity3D

    Unity3D

    2022年2月2日
    38
  • 正确的-配置Tomcat环境变量

    正确的-配置Tomcat环境变量跟着配就完事,超级正确

    2022年5月27日
    43
  • 服务器为什么要封海外,UDP攻击是什么「建议收藏」

    服务器为什么要封海外,UDP攻击是什么「建议收藏」为什么要封海外:总所周知,目前国内的大攻击大多都来自海外,因为国外的攻击成本比国内会低很多,一旦发起了攻击,并不容易找到攻击的源头。国外的家用带宽能达到千M口,咱们国内的百M口,相当于一只外国肉鸡能顶我们国内好几台肉鸡,那这个量是不得了的,而且国内的网站几乎很少有国外用户访问,目前封海外是国内的一大趋势。UDP攻击是什么:UDP攻击全称:UDP淹没攻击(UDPFloodAttack)。UDP淹没攻击是导致主机拒绝服务的一种攻击,属于带宽类攻击。UDP是一种无连接的协议,不需要用任何程序建立连接..

    2022年10月2日
    4
  • 【SpringBoot】46、SpringBoot中整合JWT实现Token验证(拦截器篇)

    【SpringBoot】46、SpringBoot中整合JWT实现Token验证(拦截器篇)前言上篇文章我们已经实现了使用自定义注解验证token信息,这样我们就会发现,当我们需要验证的接口较多时,我们需要每个方法上面都加上@JwtToken这个注解,也是非常麻烦,本片文章,我们继续使用拦截器来实现token信息的验证如果对整合JWT还不熟悉的朋友,可以先看看我的这篇博客:【SpringBoot】四十四、SpringBoot中整合JWT实现Token验证(整合篇)如果对自定义注解验证token信息感兴趣的朋友,可以看看我的这篇博客:【SpringBoot】四十五、Spr

    2022年7月25日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号