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


相关推荐

  • 流程追踪高亮图详细实现__activiti版本和flowable版本(支持驳回等)

    流程追踪高亮图详细实现__activiti版本和flowable版本(支持驳回等)在使用流程引擎的过程。如果有一张图示,可以展示流程具体到哪个环节,哪些环节已经执行过了,哪些环节没有执行过,这样子就会显得非常直观,可谓一图胜千言,流程追踪高亮图就是其中一种。一、效果展示二、源码获取activiti版本:https://github.com/wellzhi/springboot-activiti/tree/master/src/main/java/com/dapeng…

    2022年5月21日
    37
  • 查看Linux端口占用,并kill掉相关进程「建议收藏」

    话不多说,本文介绍Linux常规操作:查看端口占用进程,根据PIDkill掉相关进程。另外补充:根据程序名查看进程PID。首先,两条命令,lsof命令和netstat命令。方式一:lsof命令1、查看占用端口进程的PID:lsof-i:{端口号}2、根据PIDkill掉相关进程:kill-9{PID}方式二:net…

    2022年4月6日
    134
  • 考哪些证书国家有补贴_提交书证申请书范本

    考哪些证书国家有补贴_提交书证申请书范本当下的互联网应用如果是外网访问,一般都是https方式访问,需申请https证书目录Whyhttps?Whatishttps?Howtocreatehttps?Whyhttps?http明文传输不安全,因此需要进行加密处理,加密后端http就是httpsWhatishttps?https涉及很多概念,对称非对称加密、CA、数字签名、证书等等。有兴趣查看这里漫画解说https这里简要总结:A(客户端浏览器)——通过https://www.

    2022年10月1日
    3
  • 注册广播接收器registerReceiver

    注册广播接收器registerReceiver从registerReceiver(BroadcastReceiverreceiver,IntentFilterfilter)出发所经历的类和方法:registerReceiver(receiver,filter)–>ContextWrapper.java$registerReceiver(receiver,filter);@OverridepublicIntentregis…

    2025年7月25日
    2
  • 朋友圈集赞万能截图生成器微信小程序源码下载

    朋友圈集赞万能截图生成器微信小程序源码下载大家好这是一款朋友圈积攒截图小程序里面内涵三款样式生成,一款图文,一款分享,一款查看的样式也就是我们微信朋友圈所用到的样式就包含了里面的流量主那些可以用户自由的添加哈!赞的数量那些可以用户自定义的哈另外所需的内容也是用户自定义的安装方法的话和往常一样!直接微信开发者工具打开源码然后设置一个合法域名上传审核就可以了合法域名在压缩包里面,搭建解压了就可以看到了小程序源码下载地址:(442条消息)朋友圈集赞万能截图生成器微信小程序源码下载-小程序文档类资源-CSDN文库ht

    2022年9月6日
    4
  • laravel 5.6接入微信第三方授权登陆的主要步骤

    laravel 5.6接入微信第三方授权登陆的主要步骤

    2021年10月25日
    43

发表回复

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

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