如何在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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 揭秘成都Java培训班学费

    揭秘成都Java培训班学费Java语言从1995年诞生到现在已经走过26年时间了,由于它本身的简单易用、功能强大,加上期间不断的更新优化,Java常年保持编程语言里受欢迎排名前列的语言,这也让Java语言成为大多数人转入编程行业的首选。成都作为被列入新一线的城市,Java语言在这里自然也是发展很火热,许多小伙伴都想跻身成为一名高薪Java程序员,选择到成都编程培训机构报名学Java语言效率高是大家都知道的,但是1万多两万甚至更高的学费,让一些小伙伴望而却步,心里不禁疑虑成都Java培训班学费都由哪些组成。易牛云朗沃这就为大家揭秘。

    2022年7月7日
    36
  • react父子组件传值

    react父子组件传值react父子组件传值react父子组件传值一、父给子传值1.子组件是函数组件时,通过参数props接收2.子组件是类组件时,通过参数this.props接收二、子给父传值react父子组件传值一、父给子传值1.子组件是函数组件时,通过参数props接收2.子组件是类组件时,通过参数this.props接收二、子给父传值1.由父组件给子组件提供一个回调函数,传递给子组件;2.当子组件给父组件传值时,调用该回调函数3.父组件通过回调函数调用,拿到子组件传来的参数结果:点击按钮后

    2022年5月17日
    75
  • Idea激活码最新教程2017.2.7版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2017.2.7版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2017 2 7 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2017 2 7 成功激活

    2025年5月24日
    2
  • 宽度学习(Broad Learning System)

    宽度学习(Broad Learning System)宽度学习系统(BLS)一词的提出源于澳门大学科技学院院长陈俊龙于2018年1月发表的《BroadLearningSystem:AnEffectiveandEfficientIncrementalLearningSystemWithouttheNeedforDeepArchitecture》

    2022年5月22日
    35
  • phpstorm2021.5激活激活码(最新序列号破解)

    phpstorm2021.5激活激活码(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    61
  • VBScript教程-第二章. 运行脚本

    VBScript教程-第二章. 运行脚本因为过年,一直没有更新教程.发现按照这个进度得下个世纪能完成我这宏伟的小计划,所以最近我会加快进度.好多人问我学习方法,其实真的是学习没有捷径.最后说一句,学习脚本最好准备一份帮助文档,vbs就下载script56.chm这个文件就行了.=========================万恶的分割线后开始正题=====================…

    2022年6月17日
    19

发表回复

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

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