比较两个数字字符串值 [英] compare two numeric String values

查看:168
本文介绍了比较两个数字字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的怀疑。如果有人帮助我会很棒。

I have a simple doubt. Would be great if anyone helps me.

我有两个字符串:

String string1 = "4"; // and 
String string2 = "04";

两个值都相等,但如何在java中比较它们?我们有等于 equalsIgnoreCase 用于比较String alpha值,类似于如何比较数值。

Both the values are equal but how to compare them in java? We have equals and equalsIgnoreCase for comparing String alpha values, similarly how to compare numeric values.

推荐答案

Integer.parseInt("4") == Integer.parseInt("04")

就是这样。您可以使用 Integer.parseInt(String) 方法,返回 int 类型。然后比较与 4 == 4 相同。

That is it. You can convert a numeric string into integer using Integer.parseInt(String) method, which returns an int type. And then comparison is same as 4 == 4.

这篇关于比较两个数字字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆