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


相关推荐

  • CGLIB代理使用与原理详解

    CGLIB代理使用与原理详解JDK中提供的生成动态代理类的机制有个鲜明的特点是:某个类必须有实现的接口,而生成的代理类也只能代理某个类接口定义的方法。那么如果一个类没有实现接口怎么办呢?这就有CGLIB的诞生了,前面说的JDK的动态代理的实现方式是实现相关的接口成为接口的实现类,那么我们自然可以想到用继承的方式实现相关的代理类。【1】CGLIB简单实现①pom依赖如下&amp;amp;amp;amp;amp;amp;amp;lt;!–https://…

    2022年5月22日
    70
  • 系统可用性「建议收藏」

    系统可用性「建议收藏」一个网站、系统的战术包括可用性战术、可修改性战术、性能战术、安全性战术、可测试性战术、易用性战术。质量需求指定了软件的响应,以实现业务目标,战术是影响质量属性响应的设计决策,构架策略是战术的集合,构架

    2022年6月30日
    20
  • C++——STL中三种顺序容器的简要差别「建议收藏」

    C++——STL中三种顺序容器的简要差别

    2022年1月19日
    53
  • python修改第三方库重写_对Python第三方库,再次封装

    python修改第三方库重写_对Python第三方库,再次封装一、为何再次封装?Python一大优势是有丰富的第三方包。可以站在牛人的肩膀上,让编程更简单,功能实现更快,也更完善。但有包还不够,因为包是通用的,普遍的。包含基本功能,或为了普遍性,而给了很多选项。用起来不顺手,或不够简洁。为了更进一步提高效率,就要再次封装。就是依据具体需求,将包重新写,做个性化处理。让工具更顺手,更方便。二、举例比如在U2中,有多种方法查找元素。通过text,describt…

    2022年10月14日
    2
  • 并发编程篇:java 高并发面试题

    并发编程篇:java 高并发面试题1、线程与进程进程是一个实体。每一个进程都有它自己的地址空间,一般情况下,包括文本区域(textregion)、数据区域(dataregion)和堆栈(stackregion)。文本区域存储处理器执行的代码;数据区域存储变量和进程执行期间使用的动态分配的内存;堆栈区域存储着活动过程调用的指令和本地变量。一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成。另外,线程是…

    2022年5月6日
    41
  • Mount NTFS Partitions

    Mount NTFS PartitionsMountNTFSPartitions18January2007Windowsusesadifferentfilesystem(NTFS)tostorefiles.InorderforFedoratoreadthatfilesystem,yourequireNTFSsupportinyourkernel.Youcaneither

    2022年6月26日
    37

发表回复

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

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