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.
Don't forget the BigInteger
for very long values.
return new BigInteger(s1).compareTo(new BigInteger(s2));
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With