Skip to content
Advertisement

compare two numeric String values

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

I have two strings:

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

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.

Advertisement

Answer

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

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement