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


相关推荐

  • jmeter参数化的三种方式[通俗易懂]

    jmeter参数化的三种方式[通俗易懂]通过添加前置处理器(用户参数)通过csv参数化通过函数助手

    2022年6月15日
    27
  • Maven(二):常用命令、依赖管理

    Maven(二):常用命令、依赖管理

    2021年10月5日
    32
  • 2022年南京Java培训机构排名,实力突出遥遥领先

    2022年南京Java培训机构排名,实力突出遥遥领先2022年南京Java培训机构排名,实力突出遥遥领先近日,中国权威机构公布了2022年南京Java培训机构排名,本次排名第一的机构仍是动力节点,在2021年中,动力节点在Java培训机构排名中始终始名列前茅,那为什么动力节点在Java培训机构排名中始终名列前茅呢?一生只做一件事,动力节点为Java而生贵在专一多元化发展是人性的冲动,最容易干扰企业的方向。一个教育企业真正想要做大做强就要突破人骨子里的“贪婪”,专注到自己核心竞争力上,才会有不断提升持续进步的基础。动力节点从2009年成立至

    2022年10月3日
    0
  • django中实现逻辑删除[通俗易懂]

    django中实现逻辑删除[通俗易懂]django中实现逻辑删除,主要分三步:1.增加字段:is_delete=models.BooleanField(default=False,verbose_name=’逻辑删除’,help_text=’逻辑删除’)2.重写models中delete方法:defdelete(self,using=None,keep_parents=False):    ""…

    2022年5月15日
    42
  • 数仓分层简介(实时数仓架构)

    数仓1.数仓分层好处:复杂问题简单化;减少重复开发;隔离原始数据。2.数仓分层具体实现ODS(OperationDataStore)层:原始数据层,存原始数据,直接加载原始日志、数据DWD(DataWarehouseDetail)层:明细数据层也有叫DWI层,结构和粒度与原始表保持一致,对ODS层数据进行清洗(去除空值、脏数据、超过极限范围的数据、行式存储转列式存储、改压缩格式)DWS(DataWarehouseService)层:服务数据层,以DWD为基础进行轻度汇总。比如:用户当日

    2022年4月17日
    80
  • 正则表达式替换某个字符「建议收藏」

    正则表达式替换某个字符「建议收藏」需要匹配指定位置的某个字符示例:替换时间戳中的最后一个冒号为.号//原始字符串letstr="18:78:45:222"//匹配letreg=/^(.{5,8}):(.*)$/g//替换letresult=str.replace(reg,"$1*$2")希望有更好的方法,看官们留个言呗…

    2022年5月16日
    35

发表回复

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

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