Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare two numeric String values

Tags:

java

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.

like image 587
mee Avatar asked Apr 02 '13 12:04

mee


2 Answers

Don't forget the BigInteger for very long values.

return new BigInteger(s1).compareTo(new BigInteger(s2));
like image 129
Jin Kwon Avatar answered Sep 18 '22 17:09

Jin Kwon


Use the Integer class to convert the strings to integers.

http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29

Integer.parseInt(string1) == Integer.parseInt(string2)
like image 45
Colin D Avatar answered Sep 16 '22 17:09

Colin D